How to copy a value in a vector to next position(s) in vector

后端 未结 5 1808
余生分开走
余生分开走 2021-01-19 02:45

I have a vector that looks something like this:

c(0.5,0,0,0,0,0.7,0,0,0,0,0.4,0,0,0,0)

Suppose I want to copy the values on positions 1, 6

5条回答
  •  攒了一身酷
    2021-01-19 03:30

    Another possibility:

    vec <- c(0.5,0,0,0,0,0.7,0,0,0,0,0.4,0,0,0,0)
    
    library(zoo)
    vec[vec==0] <- NA
    na.locf(vec)
    #[1] 0.5 0.5 0.5 0.5 0.5 0.7 0.7 0.7 0.7 0.7 0.4 0.4 0.4 0.4 0.4
    

提交回复
热议问题