保存每一行的文本而不合并

时间:2018-12-27 08:29:48

标签: r dataframe text-mining

我用R写了一个代码 计算字符数后对文本进行一些更改 如果字符数大于5,则进行更改 它在这里工作是我的代码

dataset<-  c ("there is a rain " , "I am student" )
dataset <-data.frame(x= dataset)
dataset $x<-as.character(dataset $x)
words <- unlist(strsplit(dataset $x, " "))
nchar(words)
K <- character(length(words))
K[nchar(words) < 6] <- words[nchar(words) < 6]
K[nchar(words) > 5] <- gsub('e', 'X', 
                            words[nchar(words) > 5], perl = TRUE)

结果

[1] "there"   "is"      "a"       "rain"    "I"       "am"      "studXnt"

您可以看到它做了更改,但是我的问题是它在文本之间合并 因此,如果我有50行,那么我不知道文本属于哪一行 因为最后我需要将更改保存在原始文本上

我期望的结果

[1] There is a rain 
[2] I am studXnt

谢谢

1 个答案:

答案 0 :(得分:1)

这是正确的语法,

sapply(strsplit(df$x, ' '), function(i){i[nchar(i) > 5] <- gsub('e', 'X', i[nchar(i) > 5]);
                                        paste(i, collapse = ' ')})

#[1] "there is a rain" "I am studXnt"