我要缩写对象中长度超过5个字符的每个单词,并用“。”替换删除的字符。
即
x
将成为
“这是我的应试者。我在这里给了”
我想这将必须通过循环来完成,并且可能还需要拆分成单独的字符串,但是我对R非常陌生,并且真的很努力地做到这一点。任何帮助将不胜感激!
非常感谢!
我要缩写对象中长度超过5个字符的每个单词,并用“。”替换删除的字符。
即
x
将成为
“这是我的应试者。我在这里给了”
我想这将必须通过循环来完成,并且可能还需要拆分成单独的字符串,但是我对R非常陌生,并且真的很努力地做到这一点。任何帮助将不胜感激!
非常感谢!
下面是我的答案,但请考虑使用@ user20650的答案。它更加简洁和优雅(尽管如果您对正则表达式不熟悉,则可能难以理解)。根据@ user20650的第二条评论,请检查以确保其足够强大以能够处理您的实际数据。
这是一个tidyverse
选项:
library(tidyverse)
vec = c("this example sentence I have given here","and here is another long example")
vec.abbrev = vec %>%
map_chr(~ str_split(.x,pattern=" ",simplify=TRUE) %>%
gsub("(.{5}).*","\\1.",.) %>%
paste(.,collapse=" "))
vec.abbrev
[1] "this examp. sente. I have given. here" [2] "and here is anoth. long examp."
在上面的代码中,我们使用map_chr
遍历vec
中的每个句子。管道(%>%
)将每个函数的结果传递到下一个函数。
句号字符可能会造成混淆,因为它取决于上下文,具有多种含义。"(.{5}).*"
是Regular Expression,其中.
的意思是“匹配任何字符”。在"\\1."
中,.
实际上是一个句点。 .
中的最后一个gsub("(.{5}).*",.)
和.
中的第一个paste(.,collapse=" ")
是一个“代词”,代表了我们要传递给当前函数的先前函数的输出。
这是一次一次的过程:
# Split each string into component words and return as a list
vec.abbrev = str_split(vec,simplify=FALSE)
# For each sentence,remove all letters after the fifth letter in
# a word and replace with a period
vec.abbrev = map(vec.abbrev,~ gsub("(.{5}).*",.x))
# For each sentence,paste the component words back together again,# each separated by a space,and return the result as a vector,# rather than a list
vec.abbrev = map_chr(vec.abbrev,~paste(.x,collapse=" "))
,
使用for
循环,您可以执行以下操作:
x <- "this example sentence I have given here"
x2 <- unlist(strsplit(x," "))
x3 <- NULL
for(w in x2)
{
if(nchar(w) > 5) {
w <- paste0(substr(w,1,5),".")
}
else{}
x3 <- c(x3,w)
}
x_final <- paste(x3,collapse = " ")
最后的输出:
> x_final
[1] "this examp. sente. I have given here"