将NA替换为出现的下一个数字

时间:2019-01-02 12:35:19

标签: r

对于下面显示的数据,我需要用出现的下一个数字替换“问题”列中的NA。例如,时间1544890711782将是问题1,而时间1544890722157将是问题2。

数据

structure(list(participant = c("x", "x", "x", "x", "x", "x", 
"x", "x", "x", "x", "x"), time = c(1544890711782, 1544890711809, 
1544890711834, 1544890711862, 1544890711887, 1544890711899, 1544890722157, 
1544890722182, 1544890722210, 1544890722236, 1544890722245), question = c(NA, NA, NA, NA, NA, 1L, NA, NA, NA, NA, 2L)), row.names = c(8470L, 8471L, 8472L, 8473L, 8474L, 27150L, 8852L, 8853L, 8854L, 8855L, 
27134L), class = "data.frame")

enter image description here

1 个答案:

答案 0 :(得分:6)

使用来自zoo软件包的na.locf: (假设您将数据框存储在名为“数据”的对象中)

library(zoo)
data$question <- na.locf(data$question, fromLast = TRUE, na.rm = FALSE)

(fromLast设置为T:所以我们看第一个非缺失值; na.rm设置为F:这样尾随的NA不会被丢弃)

输出:

      participant         time question
8470            x 1.544891e+12        1
8471            x 1.544891e+12        1
8472            x 1.544891e+12        1
8473            x 1.544891e+12        1
8474            x 1.544891e+12        1
27150           x 1.544891e+12        1
8852            x 1.544891e+12        2
8853            x 1.544891e+12        2
8854            x 1.544891e+12        2
8855            x 1.544891e+12        2
27134           x 1.544891e+12        2