列表是R語言中的對象,它包含不一樣類型的元素,好比 - 數字,字符串,向量和另外一個列表等。一個列表還能夠包含一個矩陣或一個函數做爲它的元素。使用list()函數建立列表。函數
下面是一個例子來建立一個包含字符串,數字,向量和邏輯值的列表對象
# Create a list containing strings, numbers, vectors and a logical values. list_data <- list("Red", "Green", c(21,32,11), TRUE, 51.23, 119.1) print(list_data)
當咱們上面的代碼執行時,它產生如下結果:索引
[[1]] [1] "Red" [[2]] [1] "Green" [[3]] [1] 21 32 11 [[4]] [1] TRUE [[5]] [1] 51.23 [[6]] [1] 119.1
列表元素能夠給定它們的名字而且可使用這些名稱來訪問。element
# Create a list containing a vector, a matrix and a list. list_data <- list(c("Jan","Feb","Mar"), matrix(c(3,9,5,1,-2,8), nrow=2), list("green",12.3)) # Give names to the elements in the list. names(list_data) <- c("1st Quarter", "A_Matrix", "A Inner list") # Show the list. print(list_data)
當咱們上面的代碼執行時,它產生如下結果:字符串
$`1st_Quarter` [1] "Jan" "Feb" "Mar" $A_Matrix [,1] [,2] [,3] [1,] 3 5 -2 [2,] 9 1 8 $A_Inner_list $A_Inner_list[[1]] [1] "green" $A_Inner_list[[2]] [1] 12.3
列表的元素能夠經過在列表中的元素的索引來訪問。如遇命名列表也可使用名稱來訪問。string
咱們繼續使用在上面例子的列表it
# Create a list containing a vector, a matrix and a list. list_data <- list(c("Jan","Feb","Mar"), matrix(c(3,9,5,1,-2,8), nrow=2), list("green",12.3)) # Give names to the elements in the list. names(list_data) <- c("1st Quarter", "A_Matrix", "A Inner list") # Access the first element of the list. print(list_data[1]) # Access the thrid element. As it is also a list, all its elements will be printed. print(list_data[3]) # Access the list element using the name of the element. print(list_data$A_Matrix)
當咱們上面的代碼執行時,它產生如下結果:ast
$`1st_Quarter` [1] "Jan" "Feb" "Mar" $A_Inner_list $A_Inner_list[[1]] [1] "green" $A_Inner_list[[2]] [1] 12.3 [,1] [,2] [,3] [1,] 3 5 -2 [2,] 9 1 8
咱們能夠添加,刪除和更新列表中的元素,以下圖所示。咱們能夠增長或刪除並且只能添加到列表的末尾的元素。可是能夠更新任何元素。class
# Create a list containing a vector, a matrix and a list. list_data <- list(c("Jan","Feb","Mar"), matrix(c(3,9,5,1,-2,8), nrow=2), list("green",12.3)) # Give names to the elements in the list. names(list_data) <- c("1st Quarter", "A_Matrix", "A Inner list") # Add element at the end of the list. list_data[4] <- "New element" print(list_data[4]) # Remove the last element. list_data[4] <- NULL # Print the 4th Element. print(list_data[4]) # Update the 3rd Element. list_data[3] <- "updated element" print(list_data[3])
當咱們上面的代碼執行時,它產生如下結果:date
[[1]] [1] "New element" $NULL $`A Inner list` [1] "updated element"
能夠把全部的列表傳到一個 list()函數合併多個列表成一個列表。
# Create two lists. list1 <- list(1,2,3) list2 <- list("Sun","Mon","Tue") # Merge the two lists. merged.list <- c(list1,list2) # Print the merged list. print(merged.list)
當咱們上面的代碼執行時,它產生如下結果:
[[1]] [1] 1 [[2]] [1] 2 [[3]] [1] 3 [[4]] [1] "Sun" [[5]] [1] "Mon" [[6]] [1] "Tue"
列表能夠被轉換爲一個向量,以便能用於進一步操縱向量的元素。全部關於向量的算術運算能夠在列表被轉換爲矢量以後被應用。要作到這一點轉換,使用unlist() 函數。它以列表做爲輸入,併產生一個向量。
# Create lists. list1 <- list(1:5) print(list1) list2 <-list(10:14) print(list2) # Convert the lists to vectors. v1 <- unlist(list1) v2 <- unlist(list2) print(v1) print(v2) # Now add the vectors result <- v1+v2 print(result)
當咱們上面的代碼執行時,它產生如下結果:
[[1]] [1] 1 2 3 4 5 [[1]] [1] 10 11 12 13 14 [1] 1 2 3 4 5 [1] 10 11 12 13 14 [1] 11 13 15 17 19