我没有为每个列表编写摘要(...),而是尝试了以下代码:
test <- c('list1', 'list2')
summary(test)
因为我猜R函数读取对象而所有对象都是向量,我认为这会起作用,但事实并非如此。任何人都知道为什么这不起作用以及如何在一个命令中获取所有列表的摘要?
答案 0 :(得分:2)
您可以使用lapply
循环遍历列表中的每个元素
#Sample data
test <- list(mtcars, iris)
lapply(test, summary)
#[[1]]
# mpg cyl disp hp drat
#Min. :10.40 Min. :4.000 Min. : 71.1 Min. : 52.0 Min. :2.760
#1st Qu.:15.43 1st Qu.:4.000 1st Qu.:120.8 1st Qu.: 96.5 1st Qu.:3.080
#Median :19.20 Median :6.000 Median :196.3 Median :123.0 Median :3.695
#Mean :20.09 Mean :6.188 Mean :230.7 Mean :146.7 Mean :3.597
#3rd Qu.:22.80 3rd Qu.:8.000 3rd Qu.:326.0 3rd Qu.:180.0 3rd Qu.:3.920
#Max. :33.90 Max. :8.000 Max. :472.0 Max. :335.0 Max. :4.930
# wt qsec vs am gear
# Min. :1.513 Min. :14.50 Min. :0.0000 Min. :0.0000 Min. :3.000
#1st Qu.:2.581 1st Qu.:16.89 1st Qu.:0.0000 1st Qu.:0.0000 1st Qu.:3.000
#Median :3.325 Median :17.71 Median :0.0000 Median :0.0000 Median :4.000
#Mean :3.217 Mean :17.85 Mean :0.4375 Mean :0.4062 Mean :3.688
#3rd Qu.:3.610 3rd Qu.:18.90 3rd Qu.:1.0000 3rd Qu.:1.0000 3rd Qu.:4.000
#Max. :5.424 Max. :22.90 Max. :1.0000 Max. :1.0000 Max. :5.000
# carb
#Min. :1.000
#1st Qu.:2.000
#Median :2.000
#Mean :2.812
#3rd Qu.:4.000
#Max. :8.000
#[[2]]
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100 setosa :50
# 1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300 versicolor:50
# Median :5.800 Median :3.000 Median :4.350 Median :1.300 virginica :50
# Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199
# 3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800
# Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500
根据@docendo discimus的评论,
如果OP已经采用问题中提到的字符形式的列表。
test <- c('list1', 'list2')
在这种情况下应使用mget
lapply(mget(test), summary)