0
month  v1   v2   v3
    1   1    4   52
    1   5    2    1
    2   4  220   45
    2   6   12   21
    3   7   22   45
    3   8   22   36

I am pretty new with R. I have dataframe with date and few variable. I want to calculate the col average by month.

thelatemail
  • 91,185
  • 12
  • 128
  • 188
NEXUSAG
  • 53
  • 8

2 Answers2

3

Use data.table as it is incredibly fast, faster than dplyr or base R.

library(data.table)

meanCalc    <-  DT[ , .(mean = mean(v1, na.rm = TRUE)), by = .(month)]

You can add more columns to separate by if you add by = .(month, *other column*, ....)

You can choose a different column by switching v1 into v2 in mean(v1, ...)

black_sheep07
  • 2,308
  • 3
  • 26
  • 40
1

You can do this using dplyr

library(dplyr)
summarise(group_by(data, month), mean(v1, na.rm = TRUE), mean(v2, na.rm = TRUE), mean(v3, na.rm = TRUE))
ytk
  • 2,787
  • 4
  • 27
  • 42
  • I was trying the same but it give NA when i use the data.frame and it is not working when i converted it to numeric using as.matrix – NEXUSAG Dec 23 '15 at 00:00
  • I edited it, it shouldn't return NA now. – ytk Dec 23 '15 at 00:02
  • thank you for help!! still i got an error saying the argument is not numeric. (I think as my data set are in dataframe i am not able to calculate mean) – NEXUSAG Dec 23 '15 at 00:17
  • Use `class(data$v1)` to check what datatype it is. If it is stored as character, you can convert it to numeric with `data$v1 <- as.numeric(data$v1)`. If it is stored as factor, you can convert it to numeric with `data$v1 <- as.numeric(as.character(data$v1))`. – ytk Dec 23 '15 at 00:20