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))
I suppose this is what you were trying to accomplish:
There seems to be talk that some
dplyr
functions likerowwise
could be deprecated in the long term (such rumblings on display here). Instead, certain functions from themap
family of functions -- such as thepmap
function -- frompurrr
can be used to perform this sort of calculation:Mean is a special case (hence the use of the base function
rowMeans
), sincemean
on data.frame objects was deprecated with R 3.0.How about this?
For extra clarity, you could add another
t()
at the end:Think found a solution - just transpose your data.frame: