## Lecture 1: R basics ## Author: Adrian Waddell ## Date: Oct. 23, 2012 ## Everything behind a # is a comment ls() ## Some math with numbers x<-7 y <- 8 z <- sqrt(x^2+y^3) z ls() ## vectors c("a","b","anOtherString") v1 <- c(1.7,-8,2.9,0,-8,7/8) v2 <- c(TRUE,FALSE,TRUE,TRUE) ## matrices ?matrix ## look up matrix help page mdat <- matrix(c(1,2,3, 11,12,13), nrow = 2, ncol=3, byrow=TRUE) mdat + 1 t(mdat) # matrix transpose ls() ## lists myList <- list(mdat,v1,y,z) myList[[3]] # access third element in the list (y) ## Functions myFunction <- function(x) { y <- x-3 z <- y^2 return(z) } myFunction(x=8) myFunction # show function definition ?myFunction ## Workspace ls() ?rm rm(myFunction) rm(list = c("x","mdat")) ls() rm(v1,v2); ls() # two commands in one line are separated by an ; rm(list = ls())