## ----------------------------------------------------------------------------- b <- 10:15 b ## ----------------------------------------------------------------------------- b[1] ## ----------------------------------------------------------------------------- b[2] ## ----------------------------------------------------------------------------- b[2:3] # this is the same as b[c(2,3)] # or i <- 2:3 b[i] ## ----------------------------------------------------------------------------- b[c(1,3:6)] # or the much simpler: b[-2] ## ----------------------------------------------------------------------------- b[1] <- 11 b b[3:6] <- -99 b ## ----------------------------------------------------------------------------- b[1:3] <- 2 b b[3:6] <- c(10,20) b ## ----------------------------------------------------------------------------- m <- matrix(1:9, nrow=3, ncol=3, byrow=TRUE) colnames(m) <- c('a', 'b', 'c') m ## ----------------------------------------------------------------------------- # one value m[2,2] # another one m[1,3] ## ----------------------------------------------------------------------------- # 2 columns and rows m[1:2,1:2] # entire row m[2, ] # entire column m[ ,2] ## ----------------------------------------------------------------------------- #single column m[, 'b'] # two columns m[, c('a', 'c')] ## ----------------------------------------------------------------------------- m[2,2] # is equivalent to m[5] ## ----------------------------------------------------------------------------- m[ ,2] ## ----------------------------------------------------------------------------- m[ , 2, drop=FALSE] ## ----------------------------------------------------------------------------- # one value m[1,1] <- 5 m # a row m[3,] <- 10 m # two columns, with recycling m[,2:3] <- 3:1 m ## ----------------------------------------------------------------------------- diag(m) diag(m) <- 0 m ## ----------------------------------------------------------------------------- m <- matrix(1:9, nrow=3, ncol=3, byrow=TRUE) colnames(m) <- c('a', 'b', 'c') e <- list(list(1:3), c('a', 'b', 'c', 'd'), m) ## ----------------------------------------------------------------------------- e[2] e[[2]] ## ----------------------------------------------------------------------------- names(e) <- c('zzz', 'xyz', 'abc') ## ----------------------------------------------------------------------------- e$xyz e[['xyz']] ## ----------------------------------------------------------------------------- d <- data.frame(m) class(d) ## ----------------------------------------------------------------------------- d[,2] ## ----------------------------------------------------------------------------- d[2] ## ----------------------------------------------------------------------------- d[, 'b'] ## ----------------------------------------------------------------------------- d$b # or this d[['b']] ## ----------------------------------------------------------------------------- d['b'] ## ----------------------------------------------------------------------------- d[ , 'b', drop=FALSE] ## ----------------------------------------------------------------------------- x <- 10:20 i <- which(x > 15) x i x[i] ## ----------------------------------------------------------------------------- x <- 10:20 b <- x > 15 x b x[b] ## ----------------------------------------------------------------------------- x <- 10:20 j <- c(7,9,11,13) j %in% x which(j %in% x) ## ----------------------------------------------------------------------------- match(j, x) ## ----------------------------------------------------------------------------- match(x, j)