本文介绍了如何在R中创建具有不同数字的字符串序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是不知道如何创建一个向量,其中的字符串是常量但数字不是.例如:

I just cant figure it out how to create a vector in which the strings are constant but the numbers are not. For example:

c("raster[1]","raster[2]","raster[3]")

我想使用类似 seq(raster[1],raster[99], by=1) 之类的东西,但这不起作用.

I'd like to use something like seq(raster[1],raster[99], by=1), but this does not work.

提前致谢.

推荐答案

sprintf 函数也应该可以工作:

The sprintf function should also work:

rasters <- sprintf("raster[%s]",seq(1:99))
head(rasters)
[1] "raster[1]" "raster[2]" "raster[3]" "raster[4]" "raster[5]" "raster[6]"

正如 Richard Scriven 所建议的,%d%s 更有效.因此,如果您正在处理更长的序列,则使用以下命令会更合适:

As suggested by Richard Scriven, %d is more efficient than %s. So, if you were working with a longer sequence, it would be more appropriate to use:

rasters <- sprintf("raster[%d]",seq(1:99))

这篇关于如何在R中创建具有不同数字的字符串序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 21:47