I am a beginner with R and I have a question about simple functions such as mean or standard deviation for a big data set. My data shows monthly returns for hedge funds for the past 30 years and has 1550 columns for all hedge funds. I saw that I can calculate the mean with the mean function for a specific column by referring to the column with the name of my dataset and a $ and the no. of the column. However, I was wondering how I can get the mean for every hedge fund (which is every column) without assigning every single column. Thanks in advance for your help!
Asked
Active
Viewed 63 times
0
-
please provide some more details. Any code you tried, errors you got etc. – Sachith Muhandiram Jun 26 '16 at 09:46
-
this looks like a duplicate of http://stackoverflow.com/questions/21807987/calculate-the-mean-for-each-column-of-a-matrix-in-r Did you search it at all? – Virag Swami Jun 26 '16 at 09:49
1 Answers
1
We can use colMeans
colMeans(df1, na.rm=TRUE)
where 'df1' is the dataset.
or another option would be to loop through the columns and calculate the mean
vapply(df1, mean, na.rm=TRUE, numeric(1))
akrun
- 874,273
- 37
- 540
- 662