jeudi 20 mai 2021

Multiple conditions while grouping data with NAs and empty cells

I have a data frame as follows:

ID=c(1,1,2,3,3,1,2,4,2,1,2,1,4,3,1,2,3)
text=c("a","","R","NA","","iy","","NA","ot","ir","","NA","","","","","NA")

df <- data.frame(ID,text)
df %>% arrange(ID)

   ID text
1   1    a
2   1     
3   1   iy
4   1   ir
5   1   NA
6   1     
7   2    R
8   2     
9   2   ot
10  2     
11  2     
12  3   NA
13  3     
14  3     
15  3   NA
16  4   NA
17  4     

For each ID I have a character/ text collected. I can have NA values and/or empty values corresponding to IDs. I would like to create a binary column to present if there is text available to any of the text rows collected for an ID. I am running this code:

df %>% group_by(ID) %>% 
    summarise(text_availabe=if(any(!is.na(text))) 1 else 0)

which populates the following where for ID 3 and ID 4, it treats empty cells as they have text.

     ID text_availabe
  <dbl>         <dbl>
1     1             1
2     2             1
3     3             1
4     4             1

My idea output in this case should be like:

     ID text_availabe
  <dbl>         <dbl>
1     1             1
2     2             1
3     3             0
4     4             0

Thank you very much for your help in advance!

Aucun commentaire:

Enregistrer un commentaire