Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate the mean of some columns using dplyr::mutate

Tags:

r

dplyr

mean

I want to calculate the mean of some columns using dplyr::mutate.

library(dplyr)
test <- data.frame(replicate(12, sample(1:12, 12, rep = T))) %>% 
`colnames<-`(seq(1:12) %>% paste("BL", ., sep = ""))

The columns I want to include to calculate the mean are ONLY BL1 to BL9, so I do

test_again <- test %>% 
rowwise() %>%
mutate(ave = mean(c(seq(1:9) %>% paste("BL", ., sep = ""))))

This would not work. I notice if I put the column one by one, it works

test_againAndAgain <- test %>%
rowwise() %>%
mutate(ave = mean(c(BL1, BL2, BL3, BL4, BL5, BL6, BL7, BL8, BL9)))

I suspected it's because I give the strings instead of "columns".

Can somebody explain this behavior? What will be the best solution for this?

like image 825
analeigh Avatar asked Jan 29 '23 02:01

analeigh


1 Answers

You can use rowMeans with select(., BL1:BL9); Here select(., BL1:BL9) select columns from BL1 to BL9 and rowMeans calculate the row average; You can't directly use a character vector in mutate as columns, which will be treated as is instead of columns:

test %>% mutate(ave = rowMeans(select(., BL1:BL9)))

#   BL1 BL2 BL3 BL4 BL5 BL6 BL7 BL8 BL9 BL10 BL11 BL12      ave
#1    5  11   1   1  12   5  10  12   6   11   12    9 7.000000
#2    1  10   5  11   7   6   5   9   9    1    8    4 7.000000
#3    8  10   1   2   7  12   5   9   5    3    3   11 6.555556
#4    5   2   5   4   9   5   5   3   5    2    8    1 4.777778
#5    9   1   1  10   3   5   1   9   9    6    3   12 5.333333
#6    9   7   9   6   3   2   5   4   9    5    1    2 6.000000
#7    3   3   1   9   7   8   7   9   9   11   12    9 6.222222
#8   12   9   3   3   9  11   4   2   5   12   12   12 6.444444
#9    1   7   7  12   6   6   5   3  10   12    5   10 6.333333
#10  12   7   7   1   2   8   5   8  11    9    1    5 6.777778
#11   9   1   5   8  12   6   6  11   3   12    3    9 6.777778
#12   5   6   1  11  10  12   6   7   8    7    8    2 7.333333
like image 56
Psidom Avatar answered Feb 01 '23 10:02

Psidom