1

I have a list myList that contains 16 data.frames

List of 16

    $ dataframe1           :'data.frame':   20 obs. of  60 variables:
      ..$ Stat            : Factor w/ 16 levels "a","b",..: 16 16 16 16 16 16 16 16 16 16 ...
      ..$ P10             : num [1:31] 1 3 2 2 4 8 0 4 2 5 ...
      ..$ R               : num [1:31] NA NA NA NA NA NA NA NA NA NA ...
      ..$ S               : num [1:31] 0 7 7 1 5 9 3 9 6 2 ...

    $ dataframe2           :'data.frame':   20 obs. of  60 variables:
      ..$ Stat            : Factor w/ 16 levels "c","d",..: 16 16 16 16 16 16 16 16 16 16 ...
      ..$ P10             : num [1:31] 2 1 2 2 5 6 7 2 2 5 ...
      ..$ R               : num [1:31] NA 1 NA NA 2 NA 4 NA NA NA ...
      ..$ S               : num [1:31] 10 2 4 1 5 8 8 8 1 2 ...
...
..

I need the mean from each column and get the results in a data.frame.

       dataframe1     dataframe2     ....
 Stat  NA             NA
 P10   6              7
 R     NA             3
 S     12             14
 ...   ...            ...             ...

I have this lapply(myList$variable1, mean) which returns me the results for one data.frame in that list. ldply(myList, data.frame) does not include the mean function. How would I do that for the whole list and bring it into one data.frame?

10
  • Try sapply(myList, colMeans, na.rm=TRUE) Commented Jul 5, 2016 at 16:19
  • How is your list structured? In the output you paste above it looks highly nested---it looks like the first item in the list is a data frame named Variable1 and that data frame has a column of data frames also called Variable1... Commented Jul 5, 2016 at 16:19
  • @akrun I get Error in FUN(X[[i]], ...) : 'x' must be numeric Commented Jul 5, 2016 at 16:23
  • 1
    Why is it so hard to add a reproducible example? Commented Jul 5, 2016 at 16:26
  • 1
    @akrun thats it. The last function(y) needs to be run with function(x) though. Commented Jul 5, 2016 at 16:33

1 Answer 1

2

We can use a nested sapply. The first one will loop through the list and second will loop through the columns of the data.frame, based on the condition whether the column is numeric, we get the mean or else return NA.

sapply(myList, function(x) sapply(x, function(y)
              if(is.numeric(y)) mean(y, na.rm=TRUE) else NA))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.