how to sum variables from a table

I'm in need to recode some data that obtained using
group_by(var1,var2) %>% count()
However, I noticed that after the collapse, I just want to aggregate the categories listed by var1 that have less than 5 ocurrencies.
And that's when I failed.
If I declared something as:
table1_edited=table1 %>% mutate(var1=ifelse(var2<5,"data under 5",var1)) %>% group_by(var1,var2) %>% count()
The code above it's wrong. Because It counts the categories and not consider the previous count under the variable var2.
It's simple, but I failed every time.
Thanks for your time and interest.
Have a great day,


To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

datax %>% group_by(Categ_) %>% count()
datax %>% group_by(Categ_) %>% summarise(Sum_=sum(Freq_))

I think that code does what I tried.
That is collapsing Cateq_ while adding each obs from Freq_
Can you confirm the code is ok?
I get confused using count.
Thanks for your suggestions.

Not really sure what your intended output is as the first post didn't make that much sense, but here is your code, but tidied up a bit.


Categ_ <- c("f","f","f","s","t","t","s")  
Freq_ <- c(10,15,5,2,14,20,3) # extra ')' removed here
datax <- data.frame(Categ_,Freq_)

# put the n in with the sum
datax %>% 
  group_by(Categ_) %>% 
  summarise(Sum_=sum(Freq_), n = n())

# A tibble: 3 x 3
  Categ_  Sum_     n
  <chr>  <dbl> <int>
1 f         30     3
2 s          5     2
3 t         34     2
1 Like

The need of the code is because I tabulate some data, counting the occurrences.
But after the first 20 registers of the table, that I performed using count(), I ended up with so many occurrences containing only one observation. So, after tabulate and collapsing the counting, I then tried to recode the remaining occurrences as "other data". But doing that with group_by and count I lost the previous aggregation of cases.
Again, thanks for your code and help as always.
Have a nice day.

1 Like