rexamples.com
R Basics
http://www.rexamples.com/1/R%20Basics
How to run the code. To run the code paste it into an R console window. A - 42 A - a * 2 # R is case sensitive print(a) cat(A, n) # 84 is concatenated with n if(A a) # true, 84 42 { cat(A, , a, n) }. 1] 42 84 84 42. Don't call your variables any of the following:. Diff, length, mean, pi, range, rank, time, tree, var. If, function, NaN etc. Howard Seltman provides more information about reserved terms in this " Learning R. For variables and DoSomething. Comments start with a #. Can't be be done nicely.
rexamples.com
Match()
http://www.rexamples.com/12/Match()
How to run the code. Print(match(5, c(2,7,5,3) ) # 5 is in 3rd place print(seq(from=1,to=3,by=.5) %in% 1:3). 1] 3 [1] TRUE FALSE TRUE FALSE TRUE. Function find the first. Occurrence of the first argument in the second argument:. If no match for x is found in table. You can change this by using the nomatch. Match(1, 4:8, nomatch=-1). According to the documentation. The following two lines are equivalent:. Match(x, table, nomatch = 0) 0 x %in% table. R Language Definition (pdf). Made by Matt Zeunert.
rexamples.com
Lists
http://www.rexamples.com/7/Lists
How to run the code. Sum(0:9) append(LETTERS[1:13],letters[14:26]) c(1,6,4,9)*2 something - c(1,4,letters[2]) # indices start at one, you get (1,4,"b") length(something). Sum(0:9) [1] 45 append(LETTERS[1:13],letters[14:26]) [1] A B C D E F G H I J K L M n o [16] p q r s t u v w x y z c(1,6,4,9)*2 [1] 2 12 8 18 something - c(1,4,letters[2]) #indices start at one, you get (1,4,"b") length(something) [1] 3. You can use a colon to generate a list of numbers:. 1] -5 -4 -3 -2 -1 0 1 2 3 4 5. C(1,2,3) 3. 1] Jan...
rexamples.com
Filtering data
http://www.rexamples.com/11/Filtering%20data
How to run the code. This code uses a dataset file with population estimates. By the US Census Bureau (more info). Tbl - read.table(file.choose(),header=TRUE,sep=',') population - tbl[c(NAME,POPESTIMATE2009,NPOPCHG 2009)] smallest.state.pop - min(population$POPESTIMATE2009) print(population[population$POPESTIMATE2009= smallest.state.pop,]). NAME POPESTIMATE2009 NPOPCHG 2009 56 Wyoming 544270 11289. This piece of code extracts the data about the smallest state from the data frame. Let's use the head.
rexamples.com
Finding data sources
http://www.rexamples.com/13/Finding%20data%20sources
How to run the code. Here are a few data sets to play around with. This is a built-in data set. Use it for sorting, filtering etc. Quickly generating a data set. You can use matrix. To generate a quick dataset. The first argument must contain your data. You can use a distribution function or sample. Print(matrix(runif(6*3), nrow=6, ncol=3). Real US data: data.gov. The US government published data sets on data.gov. The UN publishes data at data.un.org. This data is also available in CSV format.
rexamples.com
Guess a random number game
http://www.rexamples.com/5/Guess%20a%20random%20number%20game
Guess a random number game. How to run the code. Guess a random number game. Use source("filename.r") to run this. Utility functions readinteger - function() { n - readline(prompt=Enter an integer: ) if(! Grepl( [0-9] $,n) { return(readinteger() } return(as.integer(n) } # real program start here num - round(runif(1) * 100, digits = 0) guess - -1 cat(Guess a number between 0 and 100. n) while(guess! N) } else if(guess num) { cat(It's smaller! Enter an integer: 20 It's bigger! Getting a random number.
rexamples.com
Reading user input
http://www.rexamples.com/4/Reading%20user%20input
How to run the code. Readinteger - function() { n - readline(prompt="Enter an integer: ") return(as.integer(n) } print(readinteger(). Enter an integer: 88 [1] 88. Lets the user enter a one-line string at the terminal. Argument is printed in front of the user input. It usually ends on ": ". The as.integer function. Makes an integer out of the string. Preventing failure if no number is entered. Right now if the user doesn't enter an integer, as.integer. Returns TRUE if the regular expression " [0-9] $".
rexamples.com
More Examples
http://www.rexamples.com/9
How to run the code. Plotting a uniform distribution. R Language Definition (pdf). Made by Matt Zeunert.
rexamples.com
Reading data
http://www.rexamples.com/10/Reading%20data
How to run the code. This code uses a dataset file with population estimates. By the US Census Bureau (more info). Tbl - read.table(file.choose(),header=TRUE,sep=,) population - tbl[POPESTIMATE2009] print(summary(population[-1:-5,]). Min 1st Qu. Median Mean 3rd Qu. Max. 544300 1734000 4141000 5980000 6613000 36960000. Reading a CSV file. Can read a variety of basic data formats into tables or "data frames". Specifies the separator for the data, which is a comma for CSV files. Is used to show a dialog.
rexamples.com
More Examples
http://www.rexamples.com/9/More%20Examples
How to run the code. Plotting a uniform distribution. R Language Definition (pdf). Made by Matt Zeunert.
SOCIAL ENGAGEMENT