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
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.
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 [ .
Of course, dplyr has 'filter()' function to do such filtering, but there is even more.
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
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