0

I have a data frame that looks like this:

enter image description here

I want to count how many different targets each microRNA gets assigned to it. So far I have tried this on R but its not working and I can't tell why

DT %>%
    group_by(miRNA) %>%
    length(unique((Target)))

I would like to get a result like this:

enter image description here

I can achieve this using other functions such aggregate and i have, i just don't understand what's wrong with my syntax's in this specific example. Any help will be very much appreciated !

1 Answers1

0

Building the dataset and adding my comment as an answer.

DT <- tibble(miRNA = c("miR-10", "miR-10", "miR-10", "miR-3", "mir-7", "mir-7", "miR-22", "miR-22", "miR-22"),
             Target = c("A", "A", "B", "C", "C", "C", "A", "A", "B"))

DT %>% 
  group_by(miRNA) %>% 
  summarise(Target = n_distinct(Target))
StephenK
  • 685
  • 5
  • 16
  • Thanks, so this one doesn't do exactly what i want because it just outputs how many unique targets there are instead of giving me the table with counts. But if use mutate instead as someone else suggested it works. Thanks ! – Amaranta_Remedios Sep 03 '20 at 19:55
  • I am not quite sure I understand the comment. The output is the same as the desired result that is listed in the question (although it is not arranged in the same order). Glad that you got it to work though. – StephenK Sep 03 '20 at 20:05