R Question - Trying to use separate to split data with a non-constant delimiter

流过昼夜 提交于 2020-01-16 09:02:28

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!