Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有向量 y=[-2 -4 -6 ... -1000],我想在每个元素之前插入一个 0 以将向量更改为:y=[0 -2 0 -4 0 -6 .. . 0 -1000]。
我该怎么做呢?我应该尝试将元素直接插入向量 y 还是创建一个只有 0 的单独向量 z 并尝试合并 2?
另外,有没有办法在不使用循环或索引向量的情况下做到这一点?
你可以使用rbind如下:
rbind
y <- seq(from=2, to=10, by=2)*-1 y # [1] -2 -4 -6 -8 -10 as.vector(rbind(0, y)) # [1] 0 -2 0 -4 0 -6 0 -8 0 -10