0

By using R studio, I have a data set like this:

ID location Bouns
1  A        yes
2  A        yes
3  B        no
4  C        yes
...

If I want to add a column by using mutate or summarize, It called 'cases' to count how many 'yes' appears in bouns base on location. For example like this

ID location Bouns cases
1  A        yes   2
2  A        yes   2
3  B        no    0
4  C        yes   1

as long as there are 2 'yes' on location A

dario
  • 6,415
  • 2
  • 12
  • 26
fatho
  • 3
  • 3

3 Answers3

0

here's the code using dplyr functions, i.e. group by and summarize:

df <- data.frame(ID = c(1,2,3,4),
                 location=c("A","A","B","C"),
                 Bouns = c("yes","yes","no","yes"))

df2 <- left_join(df,df %>% group_by(location) %>% summarise(cases=sum(Bouns=="yes")))
norimg
  • 96
  • 3
0

1.Create minimal reproducible example:

df <- structure(list(ID = 1:4, location = c("A", "A", "B", "C"),
               Bouns = c("yes", "yes", "no", "yes")),
          row.names = c(NA, -4L), class = "data.frame")

2.Since you specifically asked for the usage of mutate, here is how to use dplyr to mutate grouped df:

df %>% 
  group_by(location) %>% 
  mutate(cases = length(Bouns[Bouns == "yes"])) %>% 
ungroup()
dario
  • 6,415
  • 2
  • 12
  • 26
-1

You can try the code below using ave

df <- within(df,cases <- ave(Bouns=="yes",location, FUN = sum))

such that

> df
  ID location Bouns cases
1  1        A   yes     2
2  2        A   yes     2
3  3        B    no     0
4  4        C   yes     1
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81