【问题标题】:Loop with particular assigned numbers使用特定分配的数字循环
【发布时间】:2021-01-06 22:51:15
【问题描述】:

可能下面的问题可能非常基本,但我搜索了互联网但没有得到答案。所以我觉得堆栈溢出是正确的地方。

我想用特定数字在“R”中运行 for 循环。

ex: aa <- c(4,6,8,9)

for (i in aa) print("test")

Note: when I run loop it will run/print for 4 times only.

但我希望向量中的数字应该是循环的参数

Expected:

ex: aa <- c(4,6,8,9)

for (i in aa) print("test")

in place of aa we should get 4 . so test will print 4 times. Later 6 then again it need to print 6 times etc.

【问题讨论】:

  • 请不要发送垃圾语言标签。

标签: r loops for-loop


【解决方案1】:

我们可以使用rep

for(i in aa) print(rep("test", i))

或者可能是strrep

for(i in aa) print(strrep("test", i))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-29
    • 2021-06-28
    • 1970-01-01
    • 1970-01-01
    • 2017-05-31
    • 2018-09-12
    • 2019-10-24
    相关资源
    最近更新 更多