如何使用索引进行算术运算?

时间:2015-08-03 14:31:42

标签: r for-loop

我想创建一个循环来生成带索引的变量名。 我认为2i-1会按照我的意愿行事,但会产生错误。

Week1Day1是一个包含两行和六列的列表。

for ( i in 1:6 ) {
  paste("Week1Day1Plot",2*i-1,sep="")=conversion(Week1Day1[1,i])
}

粘贴错误(" Week1Day1Plot",2 * i - 1,sep ="")=转换(Week1Day1 [1,:   赋值目标扩展为非语言对象

1 个答案:

答案 0 :(得分:2)

使用assign

for ( i in 1:6 ) {
  assign(paste("Week1Day1Plot",2*i-1,sep=""), i)
}
ls()
[1] "i"               "Week1Day1Plot1"  "Week1Day1Plot11" "Week1Day1Plot3"
[5] "Week1Day1Plot5"  "Week1Day1Plot7"  "Week1Day1Plot9"