-1

I have a dataframe with the format

  Id_patient Evnt_chr
1         30     evnt
2         30     evnt
3         30 Not evnt
4          1 Not evnt
5          1 Not evnt
6          9     evnt
7         89     evnt
8         89 Not evnt

I need to get data frame in the following form:

Evnt_chr     Nb_id_patient  %
evnt         3              75
Not_evnt     1              25

Thank you for your help

Mathieu

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
Mathieu L
  • 73
  • 8
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Please do not post pictures of data because we cannot copy/paste the values for testing. Use a `dput()` or something more helpful. – MrFlick Mar 11 '20 at 15:59

1 Answers1

1

Using dplyr:

library(dplyr)

df %>%
  group_by(Id_patient) %>% 
  summarize(evnt = if(any(Evnt_chr == "evnt")) "evnt" else "Not evnt") %>%
  `[[`("evnt") %>%
  table() %>%
  cbind( 100 * ./sum(.)) %>%
  `colnames<-`(c("Nb_id_patient", "%"))

#>          Nb_id_patient  %
#> evnt                 3 75
#> Not evnt             1 25
Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • Thank you, sorry I didn't properly present my problem. I want to count the total number of patients who have had at least one "Evnt_chr". – Mathieu L Mar 11 '20 at 18:55