Shawn
Shawn

Reputation: 3369

Aggregate rows by group_by conditional

I would like to solve this in dplyr if possible.

I have a data frame:

Item         Count
Bike Tire    15
Car Tire     6
H. Window    3
S. Windows   7
Spare Tires  1
Widget X     88
Widget Z     34
...

I want to get the data frame to aggregate to:

Item       Count
Tires      22
H. Window  3
S. Windows 7 
Widget X   88
Widget Z   34
...

I got this far filter(data, grepl(glob2rx("*tire*"), data$Item, ignore.case = TRUE) so that I could see what rows would match, but how can I group_by so I can then aggregate the data in the example? I will also probably do the same for the other items but just want to understand the base case first.

Upvotes: 1

Views: 314

Answers (1)

akrun
akrun

Reputation: 886938

Here is one option to replace the 'Item' having 'tire' substring with 'Tires' in the group_by and get the sum of 'Count'

library(dplyr)
df1 %>%
   group_by(Item = replace(Item, grepl("Tire", Item), "Tires")) %>%
   summarise(Count = sum(Count))
# A tibble: 5 x 2
#  Item       Count
#  <chr>      <int>
#1 H. Window      3
#2 S. Windows     7
#3 Tires         22
#4 Widget X      88
#5 Widget Z      34

Upvotes: 4

Related Questions