【发布时间】:2016-02-19 19:57:44
【问题描述】:
我正在处理如下数据帧
id Comments
1 The apple fell far from the mango tree
2 I was born under a mango tree and a wandering star
3 Mules are made for packing and Mangoes for eating
我对 mango 这个词之前的 4 个词和之后的 4 个词感兴趣,包括 mango 这个词。
最终的数据集将如下所示。
id Comments
1 far from the mango tree
2 born under a mango tree and a
3 for packing and Mangoes for eating
这是测试可重现数据集
df <- read.table(text="Id,Comment
1,The apple fell far from the mango tree
2,I was born under a mango tree and a wandering star
3,Mules are made for packing and Mangoes for eating", header=T, sep=",")
关于这个非常受欢迎的任何见解
【问题讨论】:
-
前后没有4个字的情况怎么办?您要排除这些情况吗?
-
能有多个芒果吗
-
@bdeonovic 示例中似乎涵盖了这种情况,例如,id=1 后面只有一个单词。
标签: r string gsub stringr grepl