niharika Singhal niharika Singhal - 1 year ago 66
R Question

Mean for every columm in R

How,can I get the mean of each column, each column contain multiple value, and there are 1000 of column. I dont want to use loop for efficiency reason.
I have tried colmeans, rowmeans.

data format= each col with all rows is a dataframe and all these dataframes are saved in a list

For example data look like
col1 col2 .........coln
v1 c(10,11,12....) c(12,11,9....) c(12,11,9....)
v2 c(1,1,1,1,1,1...) c(1,1,1,1,1,1...) c(1,1,1,1,1,1...)
v3 c(0 ,0,1,2,3,..) c(0 ,0,2,2,2,3,..) c(0 ,0,2,2,2,3,..)
v4 c(date1,dat2,....) c(date1,dat2,....) c(date1,dat2,....)

I want to calculate the mean for V1 row for every column.


Answer Source

I got the solution, the solution for my problem is

meanValue=sapply(list_of_df[1,1:ncol(list_of_df)], function(x) mean(x))