Home > Mobile >  Any way in R to keep only columns whose rows are filtered?
Any way in R to keep only columns whose rows are filtered?

Time:10-11

In dplyr I see an argument in the mutate function which is .keep. It takes the values "all", "used", "unused", or "none". Where if I pass "used", only the calculated columns and the used columns are shown.

library(dplyr)
df <- tibble(x = c(1,2,3), 
             y = c(2,1,4),
             a = 10:12,
             b = 12:10)
df %>% mutate(z = x   y, .keep = "used")

Output:

x y z
1 2 3
2 1 3
3 4 7

Here the columns a and b are not shown.
My question: Is there any way to do the same thing when I filter some columns? I want only the columns where the filter operation is performed on the rows based on some condition.

CodePudding user response:

Let's say you are applying this filter on your dataset.

library(dplyr)
mtcars %>% filter(cyl > 4 & mpg < 15)

#                     mpg cyl disp  hp drat    wt  qsec vs am gear carb
#Duster 360          14.3   8  360 245 3.21 3.570 15.84  0  0    3    4
#Cadillac Fleetwood  10.4   8  472 205 2.93 5.250 17.98  0  0    3    4
#Lincoln Continental 10.4   8  460 215 3.00 5.424 17.82  0  0    3    4
#Chrysler Imperial   14.7   8  440 230 3.23 5.345 17.42  0  0    3    4
#Camaro Z28          13.3   8  350 245 3.73 3.840 15.41  0  0    3    4

You may save the query in a variable using mutate to make use of .keep argument and then filter it with the condition. Finally, drop the condition column from the output.

mtcars %>% 
  mutate(result = cyl > 4 & mpg < 15, .keep = "used") %>%
  filter(result) %>%
  select(-result)

#                     mpg cyl
#Duster 360          14.3   8
#Cadillac Fleetwood  10.4   8
#Lincoln Continental 10.4   8
#Chrysler Imperial   14.7   8
#Camaro Z28          13.3   8
  • Related