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) # natural log log10(4) # log in base 10 log(4, 10) # same as above sqrt(9) # square root abs(3-4) # absolute value exp(1) # exponential us.population <- 3.24e8 # From Wolfram|Alpha us.area <- 3719000 # From Wolfram|Alpha us.pop.density <- us.population / us.area us.pop.density ( us.pop.density <- us.population / us.area ) rm(us.pop.density) # gets rid of the us.pop.density variable rm(list = ls(all = TRUE)) help(sqrt) ?sqrt ?"+" example(sqrt) has.diabetes <- TRUE # logical (note case!) patient.name <- "Jane Doe" # character moms.age <- NA # used to represent an unknown ("missing") value NY.socialite.iq <- NULL # used to represent something that does not exist 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) # still NA - we still don't know! 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") colors[7] colors[7] <- "purple" indices <- 41:50 indices[1] indices[2] length(indices) state.name[indices] summary(state.area) cutoff <- 37317 state.area < cutoff small.states <- state.name[state.area < cutoff] "New York" %in% state.name[state.area < cutoff] "Rhode Island" %in% state.name[state.area < cutoff] 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.min(state.area)] seq(1, 10) # same as 1:10 seq(1, 4, 0.5) # shows all numbers from 1 to 4, incrementing by 0.5 each time ?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) # same as above 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 10 ^ (0:5) sort(state.area) # sorts the areas of the states from smallest to largest order(state.area) # returns a vector of the positions of the sorted elements state.name[order(state.area)] # sort the state names by state size state.name[order(state.area, decreasing = TRUE)] # sort the state names by state size sample(state.name, 4) # randomly picks four states sample(state.name) # randomly permute the entire vector of state names sample(state.name, replace = TRUE) # selection with replacement rev(x) # reverses the vector sum(x) # sums all the elements in a numeric or logical vector cumsum(x) # returns a vector of cumulative sums (or a running total) diff(x) # returns a vector of differences between adjacent elements max(x) # returns the largest element min(x) # returns the smallest element range(x) # returns a vector of the smallest and largest elements mean(x) # returns the arithmetic mean 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"])