Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何处理文本数据?_R - Fatal编程技术网

R 如何处理文本数据?

R 如何处理文本数据?,r,R,在R中,您有一个包含文本数据的特定数据框,例如,第二列有单词而不是数字。如何删除第二列中带有特定单词(例如“total”)的数据帧行数据您可以使用以否定。对于序列,根据您实际要查找的内容,使用seq_沿途或as.numeric(factor(.)) 以下是一些示例数据: set.seed(1) mydf <- data.frame(V1 = 1:15, V2 = sample(LETTERS[1:3], 15, TRUE)) mydf # V1 V2 # 1 1 A # 2

R
中,您有一个包含文本数据的特定数据框,例如,第二列有单词而不是数字。如何删除第二列中带有特定单词(例如“total”)的数据帧行<代码>数据您可以使用
以否定。对于序列,根据您实际要查找的内容,使用
seq_沿途
as.numeric(factor(.))

以下是一些示例数据:

set.seed(1)
mydf <- data.frame(V1 = 1:15, V2 = sample(LETTERS[1:3], 15, TRUE))
mydf
#    V1 V2
# 1   1  A
# 2   2  B
# 3   3  B
# 4   4  C
# 5   5  A
# 6   6  C
# 7   7  C
# 8   8  B
# 9   9  B
# 10 10  A
# 11 11  A
# 12 12  A
# 13 13  C
# 14 14  B
# 15 15  C
set.seed(1)

mydf非常有用!谢谢@皮波,没问题。您还应该知道,要删除多个“单词”,您需要在%
中使用
%而不是
=
,并且要删除部分匹配项(例如,匹配“tot”或“total”),您应该浏览
grepl
mydf2 <- mydf[!mydf$V2 == "A", ]
mydf2
#    V1 V2
# 2   2  B
# 3   3  B
# 4   4  C
# 6   6  C
# 7   7  C
# 8   8  B
# 9   9  B
# 13 13  C
# 14 14  B
# 15 15  C
mydf2$Seq <- ave(as.character(mydf2$V2), mydf2$V2, FUN = seq_along)
mydf2$WordAsNum <- as.numeric(factor(mydf2$V2))
mydf2
#    V1 V2 Seq WordAsNum
# 2   2  B   1         1
# 3   3  B   2         1
# 4   4  C   1         2
# 6   6  C   2         2
# 7   7  C   3         2
# 8   8  B   3         1
# 9   9  B   4         1
# 13 13  C   4         2
# 14 14  B   5         1
# 15 15  C   5         2