Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.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
将所有以n'结尾的单词替换为;R中的数据帧中的t_R - Fatal编程技术网

将所有以n'结尾的单词替换为;R中的数据帧中的t

将所有以n'结尾的单词替换为;R中的数据帧中的t,r,R,我正在寻找一种解决方案,它可以替换所有以not to两部分结尾的单词,如“has not”到“has not”或“was not”到“was not” 现在我有下面的解决方案,但我必须对所有单词都这样做,所以我想知道这可以在一行中完成: levels(txt$Comments)<-gsub("haven't", "have not",levels(txt$Comments)) levels(txt$Comments)您只需要使用正则表达式(请参阅help(“regex”)): 此外,如果

我正在寻找一种解决方案,它可以替换所有以not to两部分结尾的单词,如“has not”到“has not”或“was not”到“was not”

现在我有下面的解决方案,但我必须对所有单词都这样做,所以我想知道这可以在一行中完成:

levels(txt$Comments)<-gsub("haven't", "have not",levels(txt$Comments))

levels(txt$Comments)您只需要使用正则表达式(请参阅
help(“regex”)
):

此外,如果向量的一个元素中可能有多个这样的词,则需要
gsub()
而不是
sub()


您只需要使用正则表达式(请参见
help(“regex”)
):

此外,如果向量的一个元素中可能有多个这样的词,则需要
gsub()
而不是
sub()


下面是一个使用正则表达式
grouping()


下面是一个使用正则表达式
grouping()


谢谢,这很有效。最后,我遇到了一个小问题“不能”,它打破了“不能”。可能是第一次,我会将所有不能替换为“不能”,然后运行替换。我的文章都是评论,所以混合了多个词。@ShuhomChoudhury观点很好,这应该是一个很好的解决方法;我会把这一点添加到未来观众的答案中。谢谢,这很好用。最后,我遇到了一个小问题“不能”,它打破了“不能”。可能是第一次,我会将所有不能替换为“不能”,然后运行替换。我的文章都是评论,所以混合了多个词。@ShuhomChoudhury观点很好,这应该是一个很好的解决方法;我会在回答中为未来的观众补充这一点。
x <- c("has", "hasn't", "was", "wasn't")
sub("n't$", " not", x)

# [1] "has"     "has not" "was"     "was not"
x <- c("has", "hasn't done anything", "was", "wasn't")
sub("n't\\b", " not", x)

# [1] "has"                   "has not done anything" "was"                  
# [4] "was not"  
x <- c("has", "hasn't", "was", "I wasn't available, so we couldn't meet")
sub("n't\\b", " not", x)

# [1] "has"                                     
# [2] "has not"                                 
# [3] "was"                                     
# [4] "I was not available, so we couldn't meet"

gsub("n't\\b", " not", x)

# [1] "has"                                      
# [2] "has not"                                  
# [3] "was"                                      
# [4] "I was not available, so we could not meet"
x <- "I am not available, so we can't meet"
gsub("n't\\b", " not", x)

# [1] "I am not available, so we ca not meet"

x <- gsub("can't", "cann't", x)
gsub("n't\\b", " not", x)

# [1] "I am not available, so we can not meet"
gsub("(.*)(n\\'t)","\\1 not","haven't")
[1] "have not"