List
Lists are the R objects which contain elements of different types like − numbers, strings, vectors, matrix, functions, and another list
Index starts from 1
Create List
l = list("Red", 1, c(1, 2, 3, 4), TRUE)
Accessing
l = list("Red", 1, c(1, 2, 3, 4), TRUE)
names(l) = c("l_color", "l_num", "l_list", "l_boolean")
print(l[1]) # access by index
print(l$l_color) # access by name
Operations
l = list("Red", 1, c(1, 2, 3, 4), TRUE)
l[length(l)+1] = 100 # add element at the end
l[length(l)] = NULL # remove the last element
l[1] = "Blue" # update element
print(l)
l1 = list("Red", 1)
l2 = list("Blue", 2)
l3 = c(l1, l2) # merge lists
print(l3)
a = 1:10
b = list(a) # convert vector to list
c = unlist(b) # convert list to vector
print(c)