# I. More on vectors # 1. contain elements of just 1 type, either numeric, logical, or character # a. numeric y <- 18 y <- c(-3.6,1.58) y <- 1:9 y <- seq(from=0,to=10,by=0.02) #seq() a function w/ args from, to,.. #by argument gives distance y <- seq(0,10,by=0.02) #if know where args are, no need to specify name y <- seq(-3,25,length=6) #length option x <- c(3,3,9,12,1) y <- seq(0,10,along=x) #will return seq w/ length(x) y <- rep(x=3,times=7) #replicate the value in x, namely 3, seven times y <- rep(x=c(3,5),times=7) # same y <- rep(x=c(3,5),times=c(2,6)) #repeat 3 two times, then 5 six times # b. character y <- "San Diego" y <- c("LA","NY","San Fran") # c. logical y <- T y <- c(F,F,T) x <- 1:7 y <- (x<5) # 2. Accessing elements of a vector in 1 of 4 ways y <- c(18,32,15,-7,12,19) # a. position in vector as positive integer y[3:5] # b. excluding elements, position as negative integers y[-c(1,5,6)] # c. by element name names(y) <- c("Joe","Bill","Karen","Helen","Ray","Paul") y[c("Helen","Ray")] # d. by logical conditions y[y<15] # 3. Matrices, arrays, and factors # a. Matrices y <- c(18,32,15,-7,12,19) x <- matrix(data=y,nrow=2,ncol=3) #fill columns first (column major order) # -the default x <- matrix(data=y,nrow=2,ncol=3,byrow=T) #fill rows first dimnames(x) <- list(c("r1","r2"),c("a","b","c")) # b. Arrays # creates a 2 by 4 by 3 array y <- c(1:8, 11:18, 111:118) x <- array(y, dim = c(2,4,3)) #first dimension changes fastest # c. Factors color <- c("red", "red", "red", "green", "blue") #character valued vector colors <- factor(color, c("red","green","blue")) table(colors) # table counting occurrences of colors #one use: when building models where "dummy" variables needed # 4. Vector attributes # every vector (and object) has 2 "implicit" attributes, mode and length # some modes, e.g. "numeric", "list", "logical", "NULL" # other attributes- name, dimension,levels, class attributes(y) #for "plain" vector doesn't report anything attributes(x) attributes(colors) #II. More on lists x <- list(one=c(18:36),two=c("AK","AL","AZ"),three=c(T,T,F,T),four=matrix(1:12,3,4)) # 1. access to components x[[1]] #by order x$one #by name # 2. access to elements within components x[[1]][3:6] x$one[3:6] # 3. unlist() #convert a list to a vector unlist(x) #handy for printing out returned values from function # 4. attach(what,...) #makes components of list directly accessible attach(x) one detach(x) #no longer make this direct access possible # Note- attach(name="mydirectory",pos=2) will put a directory named # mydirectory, say, in the first position of the search list # - the directory name must be in quotes to distinguish it from an # ordinary object # 5. data frames- a special kind of list object; number of elements must # be the same for all components # - handy for regression modeling muscle <- rnorm(n=10,mean=3,sd=1) sex <- factor(rep(c("M","F"),c(6,4))) speed <- rep(0,10) speed[1:6] <- rnorm(6,30-2*muscle[1:6],2) speed[7:10] <- rnorm(4,40-2*muscle[7:10],2) mydata <- data.frame(y=speed,x1=muscle,x2=sex) temp <- lm(y~x1+x2,data=mydata) #we will work with linear models later summary(temp) #III. More on functions # 1. specified or unspecified arguments # a. unspecified c <- function(...) # b. specified mysd <- function(x) {return(sqrt(var(x)))} dumplot <- function(x,y) { plot(x,y) abline(0,1) } # 2. argument passing for specified case # if don't use names, call function by passing arguments in proper order dumplot(my.x,my.y) # if use names, then any order ok dumplot(y=my.y, x = my.x) # 3. default values for specified case dumplot <- function(x,y,my.line=T) { plot(x,y) if(my.line) { abline(lm(y~x)) } }