Lists
In this page:
Lists
A list can hold elements of different types and different lengths together, unlike a vector which requires uniform type. Elements are accessed with double brackets, list[[i]], to get the value itself, while single brackets, list[i], return a sub-list. Lists are R's flexible general-purpose container, similar to a struct or heterogeneous array in other languages.
Warning: list[1] returns a list containing the first element; list[[1]] returns the element itself -- mixing these up is a very common bug.
Example: Lists
person <- list(name = "Alice", age = 30, active = TRUE)
print(person)
cat("Name:", person[["name"]], "\n")
cat("Name (dollar syntax):", person$name, "\n")
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: