【问题标题】:How to assign of a variable to another variable with specific condition in R?如何将变量分配给R中具有特定条件的另一个变量?
【发布时间】:2020-01-13 05:04:00
【问题描述】:

假设x=c(5,6,8,9,10),我想创建另一个长度为8 的变量y3rd5th7th 位置应为零,其余位置用 x- 值填充。 预期的yc(5,6,0,8,0,9,0,10)

感谢任何帮助。

【问题讨论】:

    标签: r variables vector


    【解决方案1】:

    我们可以创建一个长度为 8 的空向量,将 x 的值分配给 y 删除 pos 处的索引。

    pos <- c(3, 5, 7)
    y <- integer(length = 8)
    y[-pos] <- x
    y
    #[1]  5  6  0  8  0  9  0 10
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多