是否有一个函数可以将列中的值扩展到NA位置?[R]



下面是我的数据示例。我正在尝试向下扩展现有的Hour_of_Day值,以填充缺失的信息。我希望在这方面得到一些指导。非常感谢。

Hour_of_Day       Counter Name People In People Out Day_of_Week Month_Day  Year
1     12:00 AM               Main         4          2    Thursday  April 01  2021
2         <NA>            FLgate1         3          2    Thursday  April 01  2021
3         <NA>   FLgate1.Counter1         0          0    Thursday  April 01  2021
4         <NA>   FLgate1.Counter2         3          2    Thursday  April 01  2021
5         <NA>   FLgate2.Counter1         0          0    Thursday  April 01  2021
6         <NA>   FLgate2.Counter2         0          0    Thursday  April 01  2021
7         <NA>            FLgate2         1          0    Thursday  April 01  2021
8         <NA> RFIDGateFL3.Aisle1         0          0    Thursday  April 01  2021
9         <NA> RFIDGateFL3.Aisle2         1          0    Thursday  April 01  2021
10        <NA> RFIDGateFL3.Aisle3         0          0    Thursday  April 01  2021
11     1:00 AM               Main         0          0    Thursday  April 01  2021
12        <NA>            FLgate1         0          0    Thursday  April 01  2021
13        <NA>   FLgate1.Counter1         0          0    Thursday  April 01  2021
14        <NA>   FLgate1.Counter2         0          0    Thursday  April 01  2021
15        <NA>   FLgate2.Counter1         0          0    Thursday  April 01  2021
16        <NA>   FLgate2.Counter2         0          0    Thursday  April 01  2021
17        <NA>            FLgate2         0          0    Thursday  April 01  2021
18        <NA> RFIDGateFL3.Aisle1         0          0    Thursday  April 01  2021

这是一个带有cumsum+is.na的基本R选项

transform(
df,
Hour_of_Day = c(NA, na.omit(Hour_of_Day))[cumsum(!is.na(Hour_of_Day)) + 1]
)

我们可以从tidyr使用fill

library(tidyr)
fill(df1, Hour_of_Day)

或使用zoo中的na.locf0

library(zoo)
df1$Hour_of_Day <- na.locf0(df1$Hour_of_Day)

相关内容

  • 没有找到相关文章

最新更新