3

I have a dataframe as follows:

Symptom                                          number        

Abdominal pain\n Swallowing probs\n Back issues\n       22
Abdominal pain\n                                        12
Back issues \n Vomiting \n                                 14
Back issues\n                                            5

There is always a \n at the end of each symptom phrase. The symptom phrase itself can literally be anything so I don't want to search for these terms specifically, but rather any term before (or between) \n

I would like to average the number for each symptom so that I end up with:

Symptom                       Avg
Abdominal pain                 17
Swallowing probs               22
Back issues                    20.5
Vomiting                       14

I don't know how to group by the individual terms with dplyr. I have tried

SypmAvg<- df %>% group_by(grepl("(?\\n.*\\n)|($.*?\\n)",df$Symptom)%>% summarise(mean=mean(number)

but it just crashes my computer so I don't even get to see the error. Can anyone help? Is it just a regex issue or is there a better way to do this?

joel.wilson
  • 8,243
  • 5
  • 28
  • 48
Sebastian Zeki
  • 6,690
  • 11
  • 60
  • 125

2 Answers2

2

We can use cSplit

library(splitstackshape)
cSplit(df, "Symptom", "\\n", "long")[, .(Avg = mean(number)), .(Symptom)]
akrun
  • 874,273
  • 37
  • 540
  • 662
1
library(dplyr)
df1 = df %>% group_by(id) %>% mutate(new_col = strsplit(Symptom, "\n")) %>% unnest()

df1 %>% group_by(trimws(new_col)) %>% summarise( ans = mean(number))

# new_col   ans
# 1   Abdominal pain 17.00000
# 2      Back issues 13.66667
# 3 Swallowing probs 22.00000
# 4         Vomiting 14.00000
joel.wilson
  • 8,243
  • 5
  • 28
  • 48