How to get the average of two columns using dplyr?

how to get average value from two columns of data table using dplyr? For example, if my data, for example, is lower:

dt <- data.table(A=1:5, B=c(1,4,NA,6,8)) 

I want to create a new column "Avg", which is the average for columns A and B for each row:

 dt %>% mutate(Avg=mean(c(A, B), na.rm=T)) 

But this code does not give me the correct result. How to do it? Thank you very much.

+5
source share
3 answers

If you want to use dplyr for this, I would suggest using the rowwise() function:

  R> library(dplyr) R> dt <- data.table(A=1:5, B=c(1,4,NA,6,8)) R> j <- dt %>% rowwise() %>% mutate(Avg=mean(c(A, B), na.rm=T)) R> j Source: local data frame [5 x 3] Groups: <by row> AB Avg (int) (dbl) (dbl) 1 1 1 1.0 2 2 4 3.0 3 3 NA 3.0 4 4 6 5.0 5 5 8 6.5 
+11
source

What about

 dt %>% mutate(Avg=rowMeans(cbind(A, B), na.rm=T)) 

mean not vectorized. It reduces all inputs to a single value. If you create a matrix with cbind() , you can use rowMeans to do the trick.

+6
source

As an initial data set, data.table we could use the data.table methods

 dt[, Avg:= mean(unlist(.SD), na.rm=TRUE) , .1:nrow(dt)] dt # AB Avg #1: 1 1 1.0 #2: 2 4 3.0 #3: 3 NA 3.0 #4: 4 6 5.0 #5: 5 8 6.5 
+1
source

Source: https://habr.com/ru/post/1237706/


All Articles