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!
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.
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.
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 ).
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)
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
#...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With