Reputation: 27
I have a list of data frames that looks like the following:
$central_nervous_system
DepMap_ID Lineage ABCA2
133 ACH-000025 central_nervous_system 0.06953841
134 ACH-000036 central_nervous_system -0.20757324
135 ACH-000040 central_nervous_system -0.07189173
ABCA3 ABCA5 ABCB9 ABCC10
133 -0.20215981 0.02591981 -0.124328522 -0.19439091
134 -0.16144270 0.08592305 -0.101500474 -0.01984359
135 -0.06166222 -0.26031989 0.009193998 -0.33360141
with 26 dataframes in total. I want to generate another list of dataframes, or a table or a list that has averages of all the columns except the first two (because they are not numerical). My approach so far has been:
lineage_avged <- lapply(x,colMeans(x[3:ncol(lineage_data)], na.rm = TRUE))
but it isn't working, i am assuming because lapply shouldn't be used here.
Upvotes: 0
Views: 374
Reputation: 39613
You can try this solution. I also include an example with other data.
lineage_avged <- lapply(YourList,function(x) colMeans(x[,-c(1,2)], na.rm = TRUE))
Example
#Create list
List <- split(iris,iris$Species)
#Function
lineage_avged <- lapply(List,function(x) colMeans(x[,-5], na.rm = TRUE))
$setosa
Sepal.Length Sepal.Width Petal.Length Petal.Width
5.006 3.428 1.462 0.246
$versicolor
Sepal.Length Sepal.Width Petal.Length Petal.Width
5.936 2.770 4.260 1.326
$virginica
Sepal.Length Sepal.Width Petal.Length Petal.Width
6.588 2.974 5.552 2.026
Upvotes: 2
Reputation: 496
Why not use apply?
library(tidyverse)
a <-tribble(~DepMap_ID, ~Lineage, ~BCA2,
"ACH-000025", "central_nervous_system", 0.06953841,
"ACH-000036", "central_nervous_system", -0.20757324,
"ACH-000040", "central_nervous_system", -0.07189173)
b <-tribble( ~ABCA3, ~ABCA5, ~ABCB9 , ~ABCC10,
-0.20215981, 0.02591981, -0.124328522, -0.19439091,
-0.16144270, 0.08592305, -0.101500474, -0.01984359,
-0.06166222, -0.26031989, 0.009193998, -0.33360141)
df <- cbind(a,b)
get_mean <- function(x){
apply(x[,-c(1,2)], 2, mean, na.rm=T)
}
To then apply it to all of your lists:
lapply(data, get_mean)
Upvotes: 0
Reputation: 4358
Example data:
x <- list(mtcars,mtcars)
Code:
sapply(x, function(df) apply(df[,-(1:2)], 2, mean))
#or use: sapply(x, function(df) colMeans(df[,-(1:2)]))
output:
[,1] [,2]
disp 230.721875 230.721875
hp 146.687500 146.687500
drat 3.596563 3.596563
wt 3.217250 3.217250
qsec 17.848750 17.848750
vs 0.437500 0.437500
am 0.406250 0.406250
gear 3.687500 3.687500
carb 2.812500 2.812500
Upvotes: 3