Rico
Rico

Reputation: 2058

R: Use elements as a list for dataframes

What I mean by this question:

Suppose my six identical dataframes are called: Jan, Feb, Mar, Apr, May and Jun.

How can I use them in a function such as apply?

The following does not work as it misses some "unquoting"?

sapply(month.abb[1:6], str)
sapply(as.list(month.abb[1:6]), str)

or

library(plyr)    
all <- rbind.fill(as.list(month.abb[1:6]))

Upvotes: 0

Views: 70

Answers (2)

Metrics
Metrics

Reputation: 15458

Solution using family of apply function:

mydata<-list(mtcars,iris[,1:4])
Map(function(x) colSums(x), mydata)
[[1]]
     mpg      cyl     disp       hp     drat       wt     qsec       vs       am     gear     carb 
 642.900  198.000 7383.100 4694.000  115.090  102.952  571.160   14.000   13.000  118.000   90.000 

[[2]]
Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
       876.5        458.6        563.7        179.9 

Upvotes: 0

mnel
mnel

Reputation: 115382

You can use get or mget to create a list containing the objects from the character strings representing their names

monthly.data <- mget(month.abb[1:6])

# now you can use `sapply  / lapply to your heart's content

Upvotes: 2

Related Questions