Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/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_Loops_Substring_Character_Abbreviation - Fatal编程技术网

有没有办法缩写R中对象的每个元素?

有没有办法缩写R中对象的每个元素?,r,loops,substring,character,abbreviation,R,Loops,Substring,Character,Abbreviation,我想缩写对象中超过5个字符的每个单词,并用“.”替换删除的字符 i、 e 我的答案在下面,但是请考虑使用@ USE20650的答案。它更加简洁和优雅(如果您不熟悉正则表达式,则可能难以理解)。根据@user20650的第二条评论,检查以确保它足够健壮,可以处理您的实际数据 这里有一个tidyverse选项: library(tidyverse) vec = c("this example sentence I have given here", "and here is anoth

我想缩写对象中超过5个字符的每个单词,并用“.”替换删除的字符

i、 e


我的答案在下面,但是请考虑使用@ USE20650的答案。它更加简洁和优雅(如果您不熟悉正则表达式,则可能难以理解)。根据@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
在上面的代码中,我们使用
map\u chr
迭代
vec
中的每个句子。管道(
%%>%%
)将每个函数的结果传递给下一个函数

句点字符可能会令人困惑,因为它有多个含义,具体取决于上下文。
“(.{5})。*”
是一个符号,其中
表示“匹配任何字符”。在
“\\1.”
中,
实际上是一个句点。
gsub(({5}.*”、“\\1.”、“
中的最后一个
paste(,,collapse=”“)
中的第一个
是一个“代词”,表示我们传递到当前函数中的前一个函数的输出

以下是一步一步的流程:

# Split each string into component words and return as a list
vec.abbrev = str_split(vec, pattern=" ", 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}).*", "\\1.", .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
gsub((?@user20650,真的很好!@user20650很好!你应该把它作为一个答案添加进去。谢谢dc37/eipi10.eipi,它适用于这个简单的例子,但我对正则表达式或可能的边缘情况没有信心,所以我会让它在这里游荡(如果你有信心,可以随意添加到你的答案中)@user20650,太棒了,谢谢!它似乎工作得很好。如果你不介意的话,如果你也能解释一下这里发生了什么,那将是令人惊讶的。我对gsub函数很熟悉,但这里的一些参数对我来说是新的,我想更好地理解以备将来使用。基本R版本将是:
sapply(strsplit(x)”),函数(x)粘贴0(sub(“({5})。*”,“\\1.”,x),collapse=“”)
我有类似的东西:
sapply(strsplit(x,”),函数(x){inds 5;x[inds]
# Split each string into component words and return as a list
vec.abbrev = str_split(vec, pattern=" ", 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}).*", "\\1.", .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=" "))