简体   繁体   English

r子字符串

[英]r substring string

gsub(".*s$", "", c("book", "books", "chair", "tables"), perl=T)

gsub(“。* s $”,“”,c(“ book”,“ books”,“ chair”,“ tables”),perl = T)

As Joran says in his comment, you are matching too much. 正如乔兰(Joran)在评论中所说,您的匹配程度太高了。 .*s$ matches any string that ends with an s . .*s$匹配以s结尾的任何字符串。 The entire string is matched, and thus the entire string is replaced with an empty string. 整个字符串都匹配,因此整个字符串将替换为空字符串。

You want this: 你要这个:

gsub("s$", "", c("book", "books", "chair", "tables"))

Note that you don't need perl=TRUE for this expression. 请注意,此表达式不需要perl=TRUE

There could well be words that end in an "s" that are not plurals and you might not want to remove the last "s". 可能会有单词以“ s”结尾而不是复数,并且您可能不想删除最后一个“ s”。 Here is another approach, using the tm package, but it stems "tables". 这是使用tm包的另一种方法,但它会产生“表”。

text <- c("book", "books", "chair", "tables", "glass", "mess")
library(tm)
text.stem <- stemDocument(text)
> text.stem
[1] "book"  "book"  "chair" "tabl"  "glass" "mess"

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

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