R Basics

Author

Matthew DeHaven

Class

January 1, 2024

Modified

March 31, 2024

1 Coding Examples from Class

x <- list(c(1, 2), c(1, NA))

lapply(x, sum, na.rm = TRUE)
[[1]]
[1] 3

[[2]]
[1] 1
sum(c(1,NA), na.rm = TRUE)
[1] 1

T and F are shorthands for TRUE and FALSE.

T == TRUE
[1] TRUE
F == FALSE
[1] TRUE
pi
[1] 3.141593
exp(1)
[1] 2.718282
x <- list(l1 = list(name1 = 5:7, name2 = c("A", "D", "E")), l2 = list(1:5, TRUE))
str(x)
List of 2
 $ l1:List of 2
  ..$ name1: int [1:3] 5 6 7
  ..$ name2: chr [1:3] "A" "D" "E"
 $ l2:List of 2
  ..$ : int [1:5] 1 2 3 4 5
  ..$ : logi TRUE
x$l1[[2]][1]
[1] "A"
x[[1]][[2]][1]
[1] "A"
x$l1$name2[1]
[1] "A"
str(x[[1]])
List of 2
 $ name1: int [1:3] 5 6 7
 $ name2: chr [1:3] "A" "D" "E"
str(x[1])
List of 1
 $ l1:List of 2
  ..$ name1: int [1:3] 5 6 7
  ..$ name2: chr [1:3] "A" "D" "E"