How can I get with dplyr the minimum (or mean) value of each row on a data.frame? I mean the same result as
apply(mydataframe, 1, mean)
apply(mydataframe, 1, min)
I've tried
mydataframe %>% rowwise() %>% mean
or
mydataframe %>% rowwise() %>% summarise(mean)
or other combinations but I always get errors, I don't know the proper way.
I know that I could also use rowMeans, but there is no simple "rowMin" equivalent. There also exist a matrixStats package but most functions don't accept data.frames, only matrixes.
If I want to calculate the min rowwise I could use
do.call(pmin, mydataframe)
Is there anything simple like this for the rowwise mean?
do.call(mean, mydataframe)
doesn't work, I guess I need a pmean function or something more complex.
Thanks
In order to compare the results we could all work on the same example:
set.seed(124)
df <- data.frame(A=rnorm(10), B=rnorm(10), C=rnorm(10))
mutate
instead ofsummarise
, by the waydo.call(pmin, mydataframe)
is a row wisemean
- try,do.call(pmin, mtcars[c("gear", "carb")])
for example, so not sure what's your issue with it – David Arenburgmtcars
datra):mtcars %>% rowwise() %>% do(data.frame(., res = mean(unlist(.), na.rm = TRUE)))
– David Arenburgas.matrix
to usematrixStats
would be pretty low. Also, something likemtcars[cbind(1:nrow(mtcars),max.col(-mtcars))]
works to find a minimum in each row. – thelatemail