1 + 2 rnorm(100) 3 - 4 5 * 6 7 / 8 1 + 2 * 3 (1 + 2) * 3 17 %/% 4 17 %% 4 2 ^ 4 2 ^ 4.3 log(4) log10(4) log(4, 10) sqrt(9) abs(3-4) exp(1) us.population <- 3.24e8 us.area <- 3719000 us.pop.density <- us.population / us.area us.pop.density ( us.pop.density <- us.population / us.area ) rm(us.pop.density) help(sqrt) ?sqrt ?"+" example(sqrt) has.diabetes <- TRUE patient.name <- "Jane Doe" moms.age <- NA NY.socialite.iq <- NULL is.enrolled <- FALSE is.candidate <- has.diabetes & ! is.enrolled TRUE & FALSE T | F T & NA F & NA TRUE | NA FALSE | NA TRUE & ! FALSE & NA as.numeric(has.diabetes) as.numeric(is.enrolled) as.character(us.population) as.character(moms.age) x <- rnorm(100) sum(x) max(x) summary(x) plot(x) hist(x) colors <- c("red", "orange", "yellow", "green", "blue", "indigo", "violet") colors <- c("infrared", colors, "ultraviolet") length(colors) colors[7] colors[7] <- "purple" a.numeric.vector <- vector(mode="numeric", length=1000) a.numeric.vector[50] <- 5 a.numeric.vector[750] <- 10 plot(a.numeric.vector) data() state.name state.area indices <- 41:50 indices[1] indices[2] length(indices) state.name[indices] # state.name[c(1:10, (length(state.name)-9):length(state.name))] summary(state.area) cutoff <- 37317 # cutoff <- summary(state.area)[2] state.area < cutoff small.states <- state.name[state.area < cutoff] "New York" %in% state.name[state.area < cutoff] "Rhode Island" %in% small.states which(state.area < cutoff) state.name[which(state.area < cutoff)] state.area[state.name == "Wyoming"] names(state.area) <- state.name state.area["Wyoming"] state.area[c("Wyoming", "Alaska")] state.area[small.states] min(state.area) state.area[which(state.area == min(state.area))] state.area[which.min(state.area)] seq(1, 10) seq(1, 4, 0.5) ?seq seq(0, 1, length.out = 10) seq(from = 1, to = 4, by = 0.5) seq(from = 0, to = 1, length.out = 10) seq(to = 99) rep(colors, 2) rep(colors, times = 2) rep(colors, each = 2) rep(colors, each = 2, times = 2) rep(colors, length.out = 10) x <- 0:9 y <- seq(from = 0, to = 90, by = 10) x + y (1:5) + y (1:4) + y y * 2 sort(state.area) order(state.area) state.name[order(state.area)] state.name[order(state.area, decreasing = TRUE)] sample(state.name, 4) sample(state.name) sample(state.name, replace = TRUE) rev(x) sum(x) cumsum(x) diff(x) max(x) min(x) range(x) mean(x) state.division levels(state.division) str(state.division) class(state.division) pony.colors <- sample(colors, size = 500, replace = TRUE) str(pony.colors) pony.colors.f <- factor(pony.colors) str(pony.colors.f) plot(pony.colors.f) pony.colors.f <- factor(pony.colors, levels = colors) str(pony.colors.f) plot(pony.colors.f) plot(state.division) state.division <- factor(state.division, levels = sort(levels(state.division))) plot(state.division) levels(state.division) levels(state.division) <- c("ENC", "ESC", "MA", "MT", "NE", "PAC", "SA", "WNC", "WSC") plot(state.division) state.name[state.division == "NE"] mean(state.area[state.division == "NE"]) / mean(state.area[state.division == "WSC"]) t.test(state.area[state.division == "SA"], state.area[state.division == "MT"]) ?USPersonalExpenditure USPersonalExpenditure rownames(USPersonalExpenditure) colnames(USPersonalExpenditure) dim(USPersonalExpenditure) USPersonalExpenditure[1,3] USPersonalExpenditure["Food and Tobacco", "1950"] USPersonalExpenditure[1, "1950"] USPersonalExpenditure[1, c(5, 3, 1)] USPersonalExpenditure["Food and Tobacco", c("1960", "1950", "1940")] USPersonalExpenditure[1, ] USPersonalExpenditure["Food and Tobacco", ] USPersonalExpenditure["Food and Tobacco", , drop = FALSE] USPersonalExpenditure[, c("1940", "1950")] USPersonalExpenditure[1:3, c("1940", "1950")] sum(USPersonalExpenditure[, "1940"]) USPersonalExpenditure[1] USPersonalExpenditure[2] USPersonalExpenditure[7] length(USPersonalExpenditure) sum(USPersonalExpenditure) game1 <- matrix(c("X", "", "O", "", "X", "O", "", "", ""), ncol = 3) game2 <- matrix(c("X", "", "O", "", "X", "O", "", "", ""), ncol = 3, byrow = TRUE) game1[3, 3] <- "X" new.game <- matrix(data = "", ncol = 3, nrow = 3) pieces <- c("rook", "knight", "bishop", "queen", "king", "bishop", "knight", "rook") pawns <- rep("pawn", 8) board <- rbind(rev(pieces), pawns, matrix("", nrow = 4, ncol = 8), pawns, pieces) rownames(board) <- 8:1 colnames(board) <- letters[1:8] USPersonalExpenditure["Personal Care", "1955"] <- "Unknown" USPersonalExpenditure rm(USPersonalExpenditure) (edu.spend <- unname(USPersonalExpenditure["Private Education", ])) (edu.yr <- seq(from = 0, to = 20, by = 5)) plot(edu.yr, edu.spend) my.model <- lm(edu.spend ~ edu.yr) my.model summary(my.model) abline(my.model) plot(my.model) str(my.model) ad.mouse.colony <- list("9.1", FALSE) ad.mouse.colony <- list(room = "9.1", bls3 = FALSE) ad.mouse.colony$conditions <- list(bedding = "straw", light_hrs = 12) ad.mouse.colony$count <- c(male = 10, female = 0) ad.mouse.colony[["variants"]] <- c("APP695swe", "PS1-dE9") names(ad.mouse.colony) ad.mouse.colony$bsl3 <- NULL x <- c(0, 1, 1, 2, 3, 5, 8, 13, 21) attr(x, "description") <- "Fibonacci series" attr(x, "description") attributes(x) str(attributes(x)) state.db <- data.frame(state.name, state.abb, state.area, state.center, stringsAsFactors = FALSE) state.db summary(state.db) head(state.db) state.db$state.abb state.db[["state.abb"]] state.db[[2]] state.db[, 2] state.db[ , 1:2] state.db[41:50, 1:2] state.db[c(50, 1), c("state.abb", "x", "y")] state.db[order(state.db$state.area)[1:5], ] state.db[order(state.db$state.area), ][1:5, ] rownames(state.db) <- state.abb state.db[c("NY", "NJ", "CT", "RI"), c("x", "y")] names(state.db) <- c("name", "abb", "area", "long", "lat") state.db$division <- state.division state.db$z.size <- (state.db$area - mean(state.db$area)) / sd(state.db$area) state.db$z.size state.db[, "z.size", drop = FALSE] state.db[state.db$area < median(state.db$area), "name"] state.db[state.db$area < median(state.db$area), "name", drop = FALSE] coastal <- state.db[state.db$division %in% c("NE", "MA", "SA", "PAC"), ] coastal <- state.db[ state.db$division %in% c("New England", "Middle Atlantic", "South Atlantic", "Pacific"), ] subset(state.db, area < median(area), select = name) coastal <- subset(state.db, division %in% c("NE", "MA", "SA", "PAC")) coastal <- subset(state.db, division %in% c("New England", "Middle Atlantic", "South Atlantic", "Pacific") ) subset(state.db, select = c(name, abb)) subset(state.db, select = -c(long, lat)) plot(area ~ division, data = state.db) plot(log(area) ~ division, data = state.db) plot(lat ~ long, data = state.db) ablation <- read.csv("Ablation.csv", header = TRUE, stringsAsFactors = TRUE) names(ablation)[names(ablation) == "SCORE"] <- "Score" write.table(ablation, file = "ablation.txt", quote = FALSE, col.names = NA, sep = "\t") write.table(ablation, file = "ablation.txt", quote = FALSE, row.names = FALSE, sep = "\t") mySummary <- function(x) { my.mean <- mean(x) my.sd <- sd(x) list(mean = my.mean, sd = my.sd) } mySummary(rnorm(100)) raiseNumber <- function(x, power = 1) { x ^ power } raiseNumber(10) raiseNumber(10, 3) num_iterations <- 100 my_means <- numeric(length = num_iterations) for (i in 1:num_iterations) { x <- rnorm(10000) my_means[i] <- mean(x) } hist(my_means)