a <- 3a is the variable, 3 is the value
b <- 4b is another variable
c <- 2c is a third variable
(a + b)^cthe result is displayed on the screen
d <- a*b/cthe result of the operation is stored in a new variable d
print(d)the value of d is printed
da shortcut for print(d)
0:10generates a series from 0 to 10
-1 + 0.2*0:10the interval is always 1 but multiplying by a real coefficient can lead to series with any interval
v <- c(2.12,5.04,3.17,77.1,11.09,62)c() concatenates a list of values
vv is a vector
w <- c(3,"hello",5,"this",7,"is",9,"a",23,"string")
wif a vector contains some string, all its elements are considered as strings as well. This is indicated by the double quotes around the "numbers" # Actually, a,b,c, and d are also vectors (which is the simplest object in R), but they contain a single element each. This is the reason why a  appears when their value is printed
3*v + v^2functions and operations apply directly to each element of a vector, and a vector of the same size is returned.
test.file <- "http://pedagogix-tagc.univ-mrs.fr/courses/statistics_bioinformatics/data/orf_lengths/yeast_orf_length_enum.txt" v <- scan(test.file)
vthe fourth element of the vector v
v[2:4]elements 2,3, and 4 are returned
v[c(2,6,8,1,3,5)]Elements are returned in the order specified within the concatenation c()
v[1:100]The 100 first elements are returned
test.table <- "http://pedagogix-tagc.univ-mrs.fr/courses/statistics_bioinformatics/data/gene_expression/gasch2000/carbon_sources/zscores_carbon_sources.tab" carbon <- read.table(test.table,header=T,row.names=1)Read a data frame from a text file. This takes a few seconds (the loading time depends on the network speed).
names(carbon)Prints the column names for a data frame.
row.names(carbon)Prints the row names for a data frame.
carbon[1,]Returns the first row, together with the header. Since there are too many columns, several lines are required.
carbon[1:10,]returns the 10 first data rows
carbon[,1]This returns the 6152 numbers that are found in the column "mannose" (these are the measures for all the genes at time point 1)
names(carbon)returns the names of the columns (column headers)
carbon[,"mannose"]Another way to get the same column, specified by its name rather than index.
carbon$mannoseYet another way to access the same column
v <- carbon[,4]The content of single column (in this case the 4th chip) can be assigned to a vector.
vAn element of the vector can then be accessed by adding a single index. This returns the value of the 3201th gene of the 4th chip.
carbon[3201,4]This directly returns the 4th chip for the 3201th gene.
carbon[100:120,1:3]This returns a rectangular section of the data frame (genes 100 to 120, chips 1 to 3).
The presence of NA values poses problems for some calculation methods. R methods generally include some options allowing to specify the way to treat these values.
One simple way to get rid of NA values is to filter out all the rows which contain one of these.
dim(carbon) carbon.filtered <- na.omit(carbon) dim(carbon.filtered)
carbon.filtered[carbon.filtered[,7] > 3,5:7]This returns columns 5 to 7 for all genes having a higher level than 3 for the 7th chip.
q()The program asks you "Save workspace image? [y/n/c]:" Answer "n" (for the time being, later it can be useful to save the state of the program between two working sessions).