R dplyr mean mean or min and other methods?

How can I get with dplyr the minimum (or average) value of each row on data.frame? I mean the same result as

apply(mydataframe, 1, mean) apply(mydataframe, 1, min) 

I tried

 mydataframe %>% rowwise() %>% mean 

or

 mydataframe %>% rowwise() %>% summarise(mean) 

or other combinations, but I always get errors, I don’t know how to correctly.

I know that I can also use rowMeans, but there is no simple equivalent to "rowMin". There is also a matrixStats package, but most functions do not accept data.frames, only matrices.

If I want to calculate the minimum number of roles, I can use do.call (pmin, mydataframe) Is there something simple like this for the middle tier? [/ P>

 do.call(mean, mydataframe) 

doesn’t work, it seems to me that I need a pmean function or something more complex.

thanks

To compare the results, we could work on one example:

 set.seed(124) df <- data.frame(A=rnorm(10), B=rnorm(10), C=rnorm(10)) 
+5
source share
3 answers

I suppose this is what you tried to accomplish:

 df <- data.frame(A=rnorm(10), B=rnorm(10), C=rnorm(10)) library(dplyr) df %>% rowwise() %>% mutate(Min = min(A, B, C), Mean = mean(c(A, B, C))) # ABC Min Mean # 1 1.3720142 0.2156418 0.61260582 0.2156418 0.73342060 # 2 -1.4265665 -0.2090585 -0.05978302 -1.4265665 -0.56513600 # 3 0.6801410 1.5695065 -2.70446924 -2.7044692 -0.15160724 # 4 0.0335067 0.8367425 -0.83621791 -0.8362179 0.01134377 # 5 -0.2068252 -0.2305140 0.23764322 -0.2305140 -0.06656532 # 6 -0.3571095 -0.8776854 -0.80199141 -0.8776854 -0.67892877 # 7 1.0667424 -0.6376245 -0.41189564 -0.6376245 0.00574078 # 8 -1.0003376 -1.5985281 0.90406055 -1.5985281 -0.56493504 # 9 -0.8218494 1.1100531 -1.12477401 -1.1247740 -0.27885677 # 10 0.7868666 0.6099156 -0.58994138 -0.5899414 0.26894694 
+5
source

How about this?

 library(dplyr) as.data.frame(t(mtcars)) %>% summarise_all(funs(mean)) 

For clarity, you can add another t() at the end:

 as.data.frame(t(mtcars)) %>% summarise_all(funs(mean)) %>% t() 
+4
source

Think about finding a solution - just rearrange your data.frame file:

 x <- data_frame(x = rnorm(10), y = rnorm(10)) # A tibble: 10 × 2 xy <dbl> <dbl> 1 -1.1240392 0.9306028477 2 -0.8213379 0.2500495105 3 -0.8289104 -0.3693704483 4 -0.6486601 -1.1421141986 5 0.5098542 -0.3703368343 6 -0.3644690 -0.0003744377 7 0.7404057 0.1166905738 8 -0.2475214 -0.0802864865 9 0.2637841 -0.7717699521 10 1.4092874 0.2998021578 x %>% t() %>% data.frame() %>% mutate_all(funs(min)) %>% unique() %>% t() 1 X1 -1.1240392 X2 -0.8213379 X3 -0.8289104 X4 -1.1421142 X5 -0.3703368 X6 -0.3644690 X7 0.1166906 X8 -0.2475214 X9 -0.7717700 X10 0.2998022 
0
source

All Articles