繁体   English   中英

将R中的字符串分隔为单词

[英]Separating a string in R into words

我正在制作一个具有以下格式类型的电影数据集:“动画|科幻”,“冒险|动画|儿童|幻想”等。

我想把它们分成单独的单词,比如“动画”和“科幻”

我已经尝试在stringr包中使用str_split ,但它没有给我我想要的东西。 我确定我使用的是错误的代码。 有人可以就如何进行给我一些建议吗? 谢谢。

编辑:我相信我应该给str_split一个正则表达式模式,所以我尝试了str_extract(test_df$genres[1:20], "\\\\w+|\\\\w+")进行测试运行,但我无法进行得到我需要的东西。

s <- "Animation|Sci-Fi|Adventure|Animation|Children|Fantasy";

# In base R
unlist(strsplit(s, "\\|"));
#[1] "Animation" "Sci-Fi"    "Adventure" "Animation" "Children"  "Fantasy"

# Using stringr
require(stringr);
unlist(str_split(s, "\\|"));
#[1] "Animation" "Sci-Fi"    "Adventure" "Animation" "Children"  "Fantasy"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM