【问题标题】:How can I use index for arithmetic? [duplicate]如何使用索引进行算术运算? [复制]
【发布时间】:2015-08-03 14:31:41
【问题描述】:

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

Week1Day1 是一个有 2 行 6 列的列表。

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

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

【问题讨论】:

  • 您可能需要?assign
  • 看看my的答案。

标签: r for-loop


【解决方案1】:

使用assign

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

【讨论】:

    猜你喜欢
    • 2013-02-27
    • 1970-01-01
    • 1970-01-01
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-05
    • 2023-03-29
    相关资源
    最近更新 更多