简体   繁体   中英

using case_when() and filter() to subset a dataframe based on values in one column and levels in another column in R

I want to filter (extract rows from) a dataframe based on values from one column while making sure all rows with the same level as the one I extracted are also extracted. Example:

condition<- rep(c("c1", "c2", "c3", "c4"), times = 4)
levelled <- c(rep("a",times = 4), rep("b", times = 4), rep("c", times = 4), rep("d", times = 4))
direction <- c(rep("up", times=10), rep("down", times = 1), rep("up", times = 5))

df <- data.frame(condition, levelled, direction)

This results in this dataframe:

   condition levelled direction
1         c1        a        up
2         c2        a        up
3         c3        a        up
4         c4        a        up
5         c1        b        up
6         c2        b        up
7         c3        b        up
8         c4        b        up
9         c1        c        up
10        c2        c        up
11        c3        c      down
12        c4        c        up
13        c1        d        up
14        c2        d        up
15        c3        d        up
16        c4        d        up

I am only interested in direction == "down" , but I want to extract all rows that have the same level in the levelled column. So my desired output df is this:

desired_output 
   condition levelled direction
9         c1        c        up
10        c2        c        up
11        c3        c      down
12        c4        c        up

In my desired_output dataframe, I extracted the row with direction == down but also the other 3 rows that have the same level in the levelled column. I think I should try something like this, but I don't know what to write on the right hand side of the tilde sign:

desired_output <- df %>% fiter(
  case_when(
    direction == "up" ~ #??
  )
)

你想要这样的东西吗?

df <- df %>% group_by(levelled) %>% filter(any(direction == "down"))

You can use -

subset(df, levelled %in% levelled[direction == 'down'])

#   condition levelled direction
#9         c1        c        up
#10        c2        c        up
#11        c3        c      down
#12        c4        c        up

In dplyr you can write this as -

library(dplyr)

df %>% filter(levelled %in% levelled[direction == 'down'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM