How to count unique values using group by and %in% in R?

Hi,

Needed advise.

In given data set,
I am trying to get couple of things:

  1. sort by column A where i need to defined ranks (100=1 and 110=2)

  2. unique values in column x where column B ="b" and B != "b" seperately using group by.
    and create new column for 1 ="b" & 0!="b"

Dataset
ab = data.frame(x = c("P1","P1","P1","P2","P2","P2","P3","P3","P3","P4","P4","P4"),
A = c("100","110","100","110","100","110","100","110","100","110","100","110"),
B = c("A1","A2","A3","A4","A5","A6","A7","A8","A9","A10","A11","A12"),
c= c("ab","ab","ab","ac","ac","ac","bb","bb","bb","bc","bc","bc"),
D= c("a","a","a","a","b","a","b","b","b","a","a","b"),
stringsAsFactors = FALSE)

Desired Output is

       x  A  B   C  D  New

1 P1 100 A1 ab a 0
2 P2 100 A5 ac b 0
3 P3 100 A7 bb b 1
4 P4 100 A11 bc a 0

Your first question is solved with arrange(), but I'm not what you want exactly in 2.
Do you mean
Count the unique values in column x for two groups defined by column D == "b" and != "b"?

Yes...that's correct

ab %>% 
  arrange(A) %>% #works because 110 lexically after 100
  mutate(bgroup = case_when(D == "b" ~ 0,   #create the grouping variable
                            TRUE ~ 1)) %>% 
  group_by(bgroup) %>% # group it
  summarize(group_size = n(),
            count_unique = n_distinct(x))  # summarize the groups.
2 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.