Reputation: 5545
Instead of writing summary(...) for each list I tried the following code:
test <- c('list1', 'list2')
summary(test)
since I guess that R functions read objects and all objects are vectors I thought this would work but it does not. Anyone knows why this is not working and how I can get the summaries of all the lists in one command?
Upvotes: 0
Views: 37
Reputation: 388817
You can use lapply
to loop over every element in the list
#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
As per the comment by @docendo discimus,
If the OP has taken lists in the form of character as mentioned in the question.
test <- c('list1', 'list2')
in that case mget
should be used
lapply(mget(test), summary)
Upvotes: 2