############# # Section 2 ############# #Accessing built-in data sets data() help(state) data(state) state.name # Checking type of vector: is.character(state.name) # extracting elements of a vector using square brackets: state.name[40] # Assignment operator <- : my.state<-state.name[40] my.state # A single equals sign also works as an assignment operator: my.state = state.name[40] #Reading a labeled numeric vector data(precip) precip names(precip) help(precip) # Various simple functions that work on vectors: length(precip) sum(precip) mean(precip) sd(precip) var(precip) ## # Recall previous question about indices: # Try this: ## as.vector(precip) #Creating a numeric vector blastoff = c(5,4,3,2,1) (blastoff) = c(5,4,3,2,1) # gives error (blastoff = c(5,4,3,2,1) ) # Surrounding an assignment code in parentheses causes it to print to the screen. # Creating simple sequences 1:100 x=3:6 10:-100 5:1 seq.length=length(seq(-10,10,0.1)); seq.length seq(-10,10,0.1) rep(1,10) scan() 1 3 5 7 2 21 3 mydata=scan() 1 3 5 7 2 21 3 # Checking what "type" or "mode" a vector is: mode(precip) is.integer(precip) is.double(precip) is(precip) mode(blastoff) is.integer(blastoff) # Doesn't check whether elements are whole numbers! is.integer pertains more to storage than appearance. is.double(blastoff) # Writing a function to check whether elements are whole numbers: is.wholenumber <- function(x, tol = .Machine$double.eps^0.5) abs(x - round(x)) < tol is.wholenumber(blastoff) all(is.wholenumber(blastoff)) is(blastoff) # Coercing the vector to be a certain type: as.integer(blastoff) is.integer(as.integer(blastoff)) state.region is(state.region) # not too informative attributes(state.region) # more accurate # More coercing: regions.cvec=as.character(state.region) regions.fvec=as.factor(regions.cvec) #2.3 Matrices data(USPersonalExpenditure) is.matrix(USPersonalExpenditure) ls() USPersonalExpenditure #Studying attributes of a matrix # A named matrix. attributes(USPersonalExpenditure) dim(USPersonalExpenditure) # Checking dimensions (rows by columns) of a matrix nrow(USPersonalExpenditure) ncol(USPersonalExpenditure) # Dimension names of this named matrix: dimnames(USPersonalExpenditure) dimnames(USPersonalExpenditure)[[2]] dimnames(USPersonalExpenditure)[[2]][3] #Extracting elements, rows and columns of a matrix USPersonalExpenditure[3,1] USPersonalExpenditure[3,] USPersonalExpenditure[,1] USPersonalExpenditure[,-4] #every column EXCEPT 4th column. USPersonalExpenditure[c(1,2,4),] USPersonalExpenditure[2:4,] USPersonalExpenditure[,"1955"] # can also pick rows or columns by their names. USPersonalExpenditure[,c("1960","1940")] USPersonalExpenditure["Private Education",] help(state) # More examples of working with a named matrix. state.x77 attributes(state.x77) dim(state.x77) length(state.x77) dim(state.x77)[1] dim(state.x77)[2] state.x77[3,5] state.x77[3,] state.x77[,5] state.x77[3,c(1,5,7)] state.x77[,"Murder"] state.x77[,"murder"] # R is case sensitive! # 2.4 Data Frames # Creating a data frame: state.dfr <- data.frame(state.name,state.region,state.abb,state.x77) state.dfr # Aspects of data frames: attributes(state.dfr) names(state.dfr) row.names(state.dfr) # referencing data frame columns: Population state.dfr$Population is.character(state.dfr$state.name) is.factor(state.dfr$state.name) state.dfr<- data.frame( I(state.name), state.region, I(state.abb), state.x77) # Repeat previous two lines of code ... # 2.5 Lists # Studying attributes of a list attlist.state.dfr<-attributes(state.dfr) attlist.state.dfr # Function outputs can be lists A <- state.x77[1:3,1:3] elistA <- eigen(A) elistA elistA[[1]] elistA[[1]][1] elistA$values # Functions: look at vecnorm.txt source("vecnorm.txt") # after changing to the correct directory... my.vec <- c(1,5,-3) vecnorm(my.vec) vecnorm(my.vec,3) ### ### # Combining vectors into matrices using 'cbind' and 'rbind': ### ### vec1 <- c(4,5,6) vec2 <- c(40,50,60) my.mat.3by2 <- cbind(vec1,vec2); my.mat.3by2 my.mat.2by3 <- rbind(vec1,vec2); my.mat.2by3 # The 'matrix' function also can create matrices; we will see more examples of this in the Section 5 notes. matrix(c(vec1,vec2), nrow=3, ncol=2, byrow=F) matrix(c(vec1,vec2), nrow=2, ncol=3, byrow=T) # Section 2.6 # Generating sequences with "by" and "length" arguments seq(from=-2.0, to=2.0, by=0.5) seq(from=-2.0, to=2.0, length=9) # Be careful about omitting argument names if you don't know the correct default order: seq(-2.0, 2.0, 0.5) seq(-2.0, 2.0, 9) seq(-2.0, 2.0, length=9) #Studying the "pretty" command seq(-2.0,2.0,length=10) pretty(seq(-2.0,2.0,length=10)) pretty(seq(-2.0,2.0,length=10),10) seq(-2.0, 2.0, 0.5) # a function that does different things, depending on what is input: A diag(A) diag(my.vec)