Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select Groups Where the First Value Equals "x" in R

I want to do a conditional slice on a grouped data set in R.

df <- data.frame(grp = c(1, 1, 2, 2, 3, 3),
                 vehicle = c("car", "boat", "bike", "car", "plane", "bike"))

#>    grp  vehicle
#> 1    1      car
#> 2    1     boat
#> 3    2     bike
#> 4    2      car
#> 5    3    plane
#> 6    3     bike

I would like to group by grp and only keep groups where the first row is equal to car. This would be the solution.

#>   grp  vehicle
#> 1   1      car
#> 2   1     boat

How do I do this? I think it could be a better version of this.

library(dplyr)
df %>% group_by(grp) %>% filter(any(slice_head(vehicle == "car")))

#, Error in `filter()`:
#, ! Problem while computing `..1 =
...
like image 424
jophuh Avatar asked Dec 05 '25 17:12

jophuh


1 Answers

It could be done without a grouping as well i.e. with duplicated to find the first occurrence of 'grp' and check if the 'vehicle' value is "car" and use that to create a logical with 'grp'

library(dplyr)
df %>% 
  filter(grp %in% grp[!duplicated(grp) & vehicle == "car"])
  grp vehicle
1   1     car
2   1    boat
like image 99
akrun Avatar answered Dec 08 '25 09:12

akrun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!