Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter "either" "or" with dplyr

Tags:

I have a data.frame that looks like this.

dat1=data.frame(time=c(1,2,1,2,1,2,1,2), team=c("A","A","A","A","B","B","C","C"), name=c("LT","LT","CH","CH","CH","CH","AT","AT"))

time team name
 1    A     LT
 2    A     LT
 1    A     CH
 2    A     CH
 1    B     CH
 2    B     CH
 1    C     AT
 2    C     AT

I would like to say to dplyr, please, group_by "team" and filter the rows of each team that contains the name "LT". If a team does not contain the name "LT", then filter in the name "CH."

I would like my data.frame to look like this.

time team name
 1    A     LT
 2    A     LT
 1    B     CH
 2    B     CH

Any help is highly appreciated

like image 521
LDT Avatar asked Nov 10 '20 18:11

LDT


People also ask

How do you filter two things in R?

In this, first, pass your dataframe object to the filter function, then in the condition parameter write the column name in which you want to filter multiple values then put the %in% operator, and then pass a vector containing all the string values which you want in the result.

What does dplyr :: filter do in R?

The filter() function is used to subset a data frame, retaining all rows that satisfy your conditions. To be retained, the row must produce a value of TRUE for all conditions. Note that when a condition evaluates to NA the row will be dropped, unlike base subsetting with [ .

Is filter a dplyr function?

Of course, dplyr has 'filter()' function to do such filtering, but there is even more.


1 Answers

One dplyr option could be:

dat1 %>%
 group_by(team) %>%
 filter(if(any(name == "LT")) name == "LT" else name == "CH")

   time team  name 
  <dbl> <fct> <fct>
1     1 A     LT   
2     2 A     LT   
3     1 B     CH   
4     2 B     CH 
like image 107
tmfmnk Avatar answered Sep 30 '22 14:09

tmfmnk