通常,R中的列表包含大量元素,每个元素可以具有不同的类型,这对于列表来说是一件好事。由于我们可以将数据类型存储为列表元素,因此存储和选择不同类型的数据变得更加容易。我们还可以一次选择列表中的单个或多个元素。这可以借助单个方括号来完成。
请看以下列表-
> list_data <- list("India", "China", c(21,32,11), letters[1:5], TRUE, + 12, letters[-c(1:10)], c(100,101,200,201), "USA", c("Asia", "Europe"), + "Education", "Poverty", "Covid-19", 365, 12, 24, 60, 7) > list_data [[1]] [1] "India" [[2]] [1] "China" [[3]] [1] 21 32 11 [[4]] [1] "a" "b" "c" "d" "e" [[5]] [1] TRUE [[6]] [1] 12 [[7]] [1] "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z" [[8]] [1] 100 101 200 201 [[9]] [1] "USA" [[10]] [1] "Asia" "Europe" [[11]] [1] "Education" [[12]] [1] "Poverty" [[13]] [1] "Covid-19" [[14]] [1] 365 [[15]] [1] 12 [[16]] [1] 24 [[17]] [1] 60 [[18]] [1] 7
选择list_data的不同元素-
> list_data[c(1,2,3)] [[1]] [1] "India" [[2]] [1] "China" [[3]] [1] 21 32 11 > list_data[c(18,2,3)] [[1]] [1] 7 [[2]] [1] "China" [[3]] [1] 21 32 11 > list_data[c(1,15,18)] [[1]] [1] "India" [[2]] [1] 12 [[3]] [1] 7 > list_data[c(5,10,15)] [[1]] [1] TRUE [[2]] [1] "Asia" "Europe" [[3]] [1] 12 > list_data[c(7,3,10)] [[1]] [1] "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z" [[2]] [1] 21 32 11 [[3]] [1] "Asia" "Europe"