# Introduction to R # by Josh Akey (April 1, 2008), modified by Joe Felsenstein (May 3, 2010) # GS560 ################################ # These R commands can be typed in, to practice R # Variables that are given names can be displayed by # simply typing their names. You can (and should) do # this to see what the results are. ################################ ################################ # R as a calculator ################################ 3^3 log10(100) log(100) exp(4) ################################ # Creating objects and assigning values # Objects are assigned using <- # Note, R is case sensitive! x and X are different ################################ # a scalar x <- 5 y <- 2 x * y z <- "I_Heart_R" # a vector created with the concatenation function (c) # must consist of the same type of data (i.e., numeric, character, logical) x12 <- c(10,6,8) x12 * 10 people <- c(seq(1,6,1)) blood.gp <- c('A', 'B', 'AB', 'B', 'O', 'O') sex <- c( rep('M', 3), rep('F', 3) ) # hey, what is seq and rep? Just ask R: At the prompt type ?rep and ?seq # accessing specific elements # the first element of the vector people people[1] # elements 1 through 3 of the vector people people[1:3] # the second element of the vector blood.gp blood.gp[2] # lists are similar to vectors but can have different data types # formally described as An R list is an ob ject consisting of an ordered collection of ob jects known as its components. doe <- list(name="john",age=28,married=F) doe names(doe) doe\$name doe\$age # another example Lst <- list(name="Fred", wife="Mary", no.children=3, child.ages=c(4,7,9)) Lst\$child.ages Lst[[4]] # Binding vectors together dai <- c(0,1,2,5,10,20) pinf <- c(0,0,0,5,25,80) row.b <- rbind(dai,pinf) col.b <- cbind(dai,pinf) # accessing columns and rows row.b[1,1] # this gets the element in row 1 column 1 row.b[,1] # this gets all elements in column 1 row.b[1,] # this gets all elements in row 1 # Matrices m1 <- matrix(1:9, byrow=T, ncol=3) m2 <- matrix(1:9, byrow=F, ncol=3) m1[,1] m1[,2] m1[1,] # data frames are a useful object and can consist of data of different types # arranged in a rectangular table. They are basically matrices that can be # composed of different data types. The components are their columns. df5 <- data.frame(dai,pinf) clinic.1 <- data.frame(people, blood.gp, sex) clinic.1\$people clinic.1\$people[1:3] # useful functions for working with dataframes dim(clinic.1) colnames(clinic.1) attributes(clinic.1) # Making an index # (recall above we made the character vector sex, consisting of M's and F's) sex # What if we wanted to just pull out the elements with an M which(sex== 'M') blokes <- which(sex== 'M') blokes sex[blokes] # How would you get the sex of the female samples? # modifying subsets of data my.vec <- c(seq(0,20,2)) my.vec index <- which(my.vector < 5) my.vec[index] my.vec[index] <- my.vec[index] + .1 my.vec # reading data in from a file data <- read.table( "filename.txt", header=T) ?read.table # writing data out to a file write.tab <- write.table(data, file = "new_file.txt", row.names = F, quote = F, sep="\t") # see what objects exist ls() # removing objects rm(blokes) # saving objects for future R sessions save.image() # you are asked whether you want to do this when you quit # quit R q()