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.31e8 # From Wolfram|Alpha, 2020 estimate 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) 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) x <- rnorm(100) sum(x) max(x) summary(x) plot(x) hist(x) my_colors <- c("red", "orange", "yellow", "green", "blue", "indigo", "violet") colors <- c("infrared", colors, "ultraviolet") my_colors <- c("infrared", my_colors, "ultraviolet") length(my_colors) my_colors[7] my_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% small_states "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(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(my_colors, 2) rep(my_colors, times = 2) # same as above rep(my_colors, each = 2) rep(my_colors, each = 2, times = 2) rep(my_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(my_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 = my_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"]) rm(state.division)