问题
One of the variables is participant age groups, an example of one of the records is shown below,
0::Adult 18+||1:: Adult 18+||2::Adult 18+||3::Child 0-11
How do you best split this out so that it will give Adult 18 + with the result of 3 and Child 0-11 with 1?
I tried using separate, but as the delimiter is not constant, it was omitting a lot of the records. Any suggestions would be helpful, thank you! As this is my first post, let me know if I need to add more information.
回答1:
Here is one way:
library(magrittr)
vals <- "0::Adult 18+||1:: Adult 18+||2::Adult 18+||3::Child 0-11"
strsplit(gsub("[^[:alpha:][:space:]]","", vals), "\\s+") %>% as.data.frame() %>% table()
Adult Child
3 1
来源:https://stackoverflow.com/questions/58677011/r-question-trying-to-use-separate-to-split-data-with-a-non-constant-delimiter