简体   繁体   English

如何将 for 循环的结果保存为字符串?

[英]How do I save the results of a for-loop as a character string?

I've managed to automate some tedious code-writing with something like the following:我已经成功地用类似下面的东西自动化了一些乏味的代码编写:

codes<-c("code1", "code2","code3")
for(i in codes){print(paste0("repetitivetext",i))}

yielding something like the following output:产生类似于以下 output 的内容:

"repetitivetextcode1"
"repetitivetextcode2"
"repetitivetextcode3"

Now I want to add the beginning and end of the code.现在我想添加代码的开头和结尾。 I write:我写:

paste0("beginning",for(i in codes){print(paste0("repetitivetext",i))},"end")¨

Hoping to get something like:希望得到类似的东西:

beginningrepetitivetextcode1repetitivetextcode2repetitivetextcode3end

Instead I get:相反,我得到:

"repetitivetextcode1"
"repetitivetextcode2"
"repetitivetextcode3"
"beginningend"

How do I get my desired output?如何获得我想要的 output? Is there for instance a way of collapsing the output of the for loop into a single character string (I already tried the collapse-option in paste0)?例如,是否有一种方法可以将 for 循环的 output 折叠成单个字符串(我已经在 paste0 中尝试过折叠选项)?

This code segment will then have to be pasted together with other similarly created segments, so the lines must be saved in the correct order and they need to be saved as a single character string.此代码段必须与其他类似创建的段粘贴在一起,因此必须以正确的顺序保存这些行,并且需要将它们保存为单个字符串。

First, define an empty vector output to hold the result of the for loop ( iff you want to use one, as there are more economical solutions readily available as noted by others):首先,定义一个空向量output来保存for循环的结果(如果你想使用一个,因为有其他人提到的更经济的解决方案):

output <- c()
for(i in codes){
  output[i] <- paste0("repetitivetext",i)
  }

Then simply paste0 the text elements around output :然后简单地output paste0周围的文本元素:

paste0("beginning", output, ,"end")
[1] "beginningrepetitivetextcode1end" "beginningrepetitivetextcode2end" "beginningrepetitivetextcode3end"

If you want to have it all in one chunk, add the collapse argument:如果你想把它全部放在一个块中,添加collapse参数:

paste0("beginning",output,"end", collapse = "")
[1] "beginningrepetitivetextcode1endbeginningrepetitivetextcode2endbeginningrepetitivetextcode3end"

Data:数据:

codes<-c("code1", "code2","code3")

collapse without the loop should do:没有循环的collapse应该这样做:

paste0('beginning',
        paste0('repetitivetext', codes, collapse = ''),
        'end'
)

output: output:

## [1] "beginningrepetitivetextcode1repetitivetextcode2repetitivetextcode3end"

Actaully for loops in R in certain cases can be replaced by vectorized functions:在某些情况下,R 中的 Actaully for循环可以用向量化函数代替:

codes <- c("code1", "code2", "code3")
codes2 <- paste0("repetitivetext", codes)
print(codes2)

Here is the output:这是 output:

[1] "repetitivetextcode1" "repetitivetextcode2" "repetitivetextcode3"

And the resulting string can be also acheived without any loops:结果字符串也可以在没有任何循环的情况下实现:

paste0("beginning", 
       paste0(codes2, collapse = ""), 
       "end"
)

And you get the following output:你得到以下 output:

[1] "beginningrepetitivetextcode1repetitivetextcode2repetitivetextcode3end"

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

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