1

table, data.frame.

Which has 12 columns with variable names and 24 rows df

Like:

Var1 Var2 Var3 Var4 Var12
1     NA   2     3    4
5     6    2     3    3
NA    7    8     NA   4

And I want to calculate the mean for each column while ignoring the Na's For example:

colMeans(df)

And get the result like:

Var1  Var2  Var3  Var4  Var12
 3     6,5   4     3     3,66

I don't want the NA's to be considered in the calculation of Mean.

I tried some methods like na.omit, !is.na, but I don't get the desired result like what I described above.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
Alex Rika
  • 141
  • 2
  • 14

1 Answers1

1

For a data.table dt, that looks like this:

dt
   Var1 Var2 Var3 Var4 Var12
1:    1   NA    2    3     4
2:    5    6    2    3     3
3:   NA    7    8   NA     4

You can simply use lapply():

dt[, lapply(.SD, mean, na.rm = TRUE)]

The result is:

   Var1 Var2 Var3 Var4    Var12
1:    3  6.5    4    3 3.666667
clemens
  • 6,653
  • 2
  • 19
  • 31