1. 程式人生 > >R語言 list注意事項

R語言 list注意事項

  1. 如果列表中間的某一元素沒有輸入,則將該元素自動儲存為NULL
a <- NULL
a[[1]] <- matrix(1:12, ncol = 3)
a[[3]] <- c("B","C","F")

結果輸出如下:

a
[[1]]
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
[[2]]
NULL
[[3]]
[1] “B” “C” “F”

  1. 將列表中某一元素置為NULL,則將列表長度自動縮減,將那個NULL元素自動抹除
a <- NULL
a[[1]] <- matrix(1:12, ncol = 3)
a[[2]] <- 1:4
a[[3]] <- c("B","C","F")
a[[2]] <- NULL
length(a)
a

結果輸出如下:

length(a)
[1] 2
a
[[1]]
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
[[2]]
[1] “B” “C” “F”