R问题-尝试使用非恒定定界符分隔数据 [英] R Question - Trying to use separate to split data with a non-constant delimiter
本文介绍了R问题-尝试使用非恒定定界符分隔数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
其中一个变量是参与者年龄组,下面是其中一个记录的示例。
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
您如何最好地将其拆分为成人18 +,结果为3,儿童0-11,结果为1?
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.
推荐答案
这里是一种方法:
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
这篇关于R问题-尝试使用非恒定定界符分隔数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文