【发布时间】:2014-01-30 13:56:43
【问题描述】:
我有一个命令,它在 R 中每 10 个循环生成一个变量(索引 1、索引 2、索引 3 ......等等)。我拥有的命令是有效的,但我正在考虑一种更聪明的方式来编写这个命令。这是我的命令的样子:
for (counter in 1:10){
for (i in 1:100){
if (counter == 1){
index1 <- data1 ## some really long command here, I just changed it to this simple command to illustrate the idea
}
if (counter == 2){
index2 <- data2
}
.
.
.
# until I reach index10
} indexing closure
} ## counter closure
有没有办法写这个而不必写条件 if 命令?我想生成 index1, index2.... 我确信有一些简单的方法可以做到这一点,但我就是想不出。
谢谢。
【问题讨论】:
-
也许使用向量?
-
他们必须是
index1等吗?为什么不将它们附加到向量中?你打算如何使用它们? -
我试图做类似 index[counter]
标签: r variables for-loop counter naming-conventions