Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get pmax over multiple variables with dplyr? [duplicate]

Tags:

r

dplyr

Before someone marks this question as duplicate, I have already seen this one and it does not solve my question. If I try

mtcars %>% mutate(new = rowMeans(select(.,c(1,7)), na.rm = TRUE))

it works nicely, but if I do the same with pmax instead of rowMeans:

mtcars %>% mutate(new = pmax(select(.,c(1,7)), na.rm = TRUE))

I get

Error: Column `new` is of unsupported class data.frame

Why? In this example, I can get the output with

mtcars %>% mutate(new = pmax(mpg,qsec,carb,na.rm = TRUE))

but I try to use select since I need for my real data either some select helper or variables determined by column position (like 1,7 in the example), and otherwise I also get errors.

As suggested in an answer in the linked question I also tried to use do.call obtaining an error too.

Thank you!

like image 788
iago Avatar asked Sep 04 '19 12:09

iago


People also ask

How many rows met both conditions in the dplyr filter function?

Only one row met both conditions in the filter function. library(dplyr) #filter where team is equal to 'A' and points > 89 and assists < 30 df %>% filter (team == 'A' & points > 89 & assists < 30) team points assists rebounds 1 A 90 28 28 Note: You can find the complete documentation for the dplyr filter () function here.

How to remove duplicate rows based on multiple columns using dplyr?

In this article, we will learn how to remove duplicate rows based on multiple columns using dplyr in R programming language. distinct () function can be used to filter out the duplicate rows. We just have to pass our R object and the column name as an argument in the distinct () function.

How to select some columns without typing whole names in dplyr?

For selecting some columns without typing whole names when using dplyr I prefer select parameter from subset function. iris %>% subset (select = 2:4) %>% mutate (mak = do.call (pmax, (.))) %>% select (mak) %>% cbind (iris) I think one can just do select ( 2:4 ) instead of subset ( select = 2:4 ).

How to use rowmaxs with PMX in R?

One approach is to pipe the data into select then call pmax using a function that makes pmax rowwise (this is very similar to @inscaven's answer that uses do.call, unfortunately there isn't a rowMaxs function in R so we have to use a function to make pmax rowwise -- below I used purrr::pmap)


Video Answer


1 Answers

Using do.call we can evaluate pmax without specifying the variables, i.e.

mtcars %>% 
  mutate(new = do.call(pmax, c(select(., c(1, 7)), na.rm = TRUE)))

#    mpg cyl  disp  hp drat    wt  qsec vs am gear carb   new
#1  21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4 21.00
#2  21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4 21.00
#3  22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1 22.80
#4  21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1 21.40
#5  18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2 18.70
#6  18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1 20.22
#7  14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4 15.84
#...
like image 92
Sotos Avatar answered Sep 30 '22 01:09

Sotos