R Dataframe :在满足条件的地方填充特定数量的行

9avjhtql  于 2022-12-20  发布在  其他
关注(0)|答案(1)|浏览(94)

我在R中工作,我有一个 Dataframe ,其中有一个DateTime列和一个Binary列,按时间标记一个事件,如示例 Dataframe 所示:

DateTime <- seq(from = as.POSIXct("2021-01-01 00:00:00"), to = as.POSIXct("2021-01-01 17:00:00"), by = "hour")
Binary <- c(NA, 1, rep(NA, 5), 1, rep(NA, 5), 1, rep(NA, 4))
sample <- data.frame(DateTime, Binary)

我想创建一个新列,分配“H”,其中“1”在Binary列中指示,以及x个行数,其中“1”指示。对于此示例,上面和下面各1行,如“目标”数据框所示:

Height <- c(rep('H', 3), rep(NA, 3), rep('H',3), rep(NA, 3), rep('H', 3), rep(NA, 3))
goal <- data.frame(DateTime, Binary, Height)

我可以使用for循环来实现这一点,但是,由于我拥有的实际数据集非常大(几乎有100万个观测值),所以速度非常慢,下面是我使用的for循环的一个例子:

# create new column Height
sample$Height <- NA
# Use a for loop to assign H
  for (i in 1 : length(sample$Height)){
    if(sample$Binary[i] %in% c(1)){sample$Height [i] <- "H"}
    if(sample$Binary[i] %in% c(1)){sample$Height [i+1] <- "H"}
    if(sample$Binary[i] %in% c(1)){sample$Height [i-1] <- "H"}
  }

我可以使用dplyr在Binary列中有“1”的行中赋值“H”。

sample <- sample %>%
  mutate(Height = ifelse(sample$Binary==1,'H', NA))

但是,有没有办法也填充指定数量的行(在此之上和之下)?
我还考虑过在上面的步骤之后使用fill():

sample <- fill(sample$Height, .direction="updown")

但当然,这填补了所有NA的,我不希望...

bvuwiixz

bvuwiixz1#

DateTime <- seq(from = as.POSIXct("2021-01-01 00:00:00"), to = as.POSIXct("2021-01-01 17:00:00"), by = "hour")
Binary <- c(NA, 1, rep(NA, 5), 1, rep(NA, 5), 1, rep(NA, 4))
sample <- data.frame(DateTime, Binary)

Height <- c(rep('H', 3), rep(NA, 3), rep('H',3), rep(NA, 3), rep('H', 3), rep(NA, 3))
goal <- data.frame(DateTime, Binary, ExpectedHeight = Height)

library(dplyr)    

goal %>% 
  mutate(
    Height = case_when(
      Binary | lag(Binary) | lead(Binary)  == 1 ~ "H",
      TRUE ~ NA_character_
    )
  )

              DateTime Binary ExpectedHeight Height
1  2021-01-01 00:00:00     NA              H      H
2  2021-01-01 01:00:00      1              H      H
3  2021-01-01 02:00:00     NA              H      H
4  2021-01-01 03:00:00     NA           <NA>   <NA>
5  2021-01-01 04:00:00     NA           <NA>   <NA>
6  2021-01-01 05:00:00     NA           <NA>   <NA>
7  2021-01-01 06:00:00     NA              H      H
8  2021-01-01 07:00:00      1              H      H
9  2021-01-01 08:00:00     NA              H      H
10 2021-01-01 09:00:00     NA           <NA>   <NA>
11 2021-01-01 10:00:00     NA           <NA>   <NA>
12 2021-01-01 11:00:00     NA           <NA>   <NA>
13 2021-01-01 12:00:00     NA              H      H
14 2021-01-01 13:00:00      1              H      H
15 2021-01-01 14:00:00     NA              H      H
16 2021-01-01 15:00:00     NA           <NA>   <NA>
17 2021-01-01 16:00:00     NA           <NA>   <NA>
18 2021-01-01 17:00:00     NA           <NA>   <NA>

相关问题