简体   繁体   English

数据框中的条件填充值

[英]Conditional fill value in Dataframe

I have a dataframe which look like: 我有一个数据框,看起来像:

Date        Avc1     Xrt2      Var3     Var4
2017-04-04  0        0         0        0
2017-04-04  10       18        22       25
2017-04-04  8        12        16       20
2017-04-04  5        10        13       18

Now i want to conditionally fill Var3 and Var4 value with exact below value whenever its 0. 现在我想有条件地填充Var3Var4值,使其精确地低于0。

Output: 输出:

Date        Avc1     Xrt2      Var3     Var4
2017-04-04  0        0         22       25
2017-04-04  10       18        22       25
2017-04-04  8        12        16       20
2017-04-04  5        10        13       18

Using: 使用方法:

df[4:5] <- lapply(df[4:5], function(x) {i <- which(x == 0); x[i] <- x[i+1]; x})

gives: 给出:

> df
        Date Avc1 Xrt2 Var3 Var4
1 2017-04-04    0    0   22   25
2 2017-04-04   10   18   22   25
3 2017-04-04    8   12   16   20
4 2017-04-04    5   10   13   18

Another possibility with na.locf from the zoo -package: zoo na.locf另一种可能性:

i <- which(df == 0, arr.ind = TRUE)

df[i[i[,2] %in% 4:5,]] <- NA

df <- zoo::na.locf(df, fromLast = TRUE)

Here are a few options. 这里有一些选择。 In the first 2, I used mutate_at to remove 0s from those columns, then use fill from tidyr to fill upwards. 在第2,我用mutate_at从这些列中移除0,则使用filltidyr填补向上。

In the third, I used lead to take the value from below if the value in that column is 0. 在第三个示例中,如果该列中的值为0,则使用lead从下面获取值。

library(tidyverse)

df %>%
    mutate_at(vars(Var3, Var4), function(x) ifelse(x == 0, NA, x)) %>%
    fill(Var3, Var4, .direction = "up")
#> # A tibble: 4 x 5
#>   Date        Avc1  Xrt2  Var3  Var4
#>   <date>     <int> <int> <int> <int>
#> 1 2017-04-04     0     0    22    25
#> 2 2017-04-04    10    18    22    25
#> 3 2017-04-04     8    12    16    20
#> 4 2017-04-04     5    10    13    18

df %>%
    mutate_at(vars(Var3, Var4), na_if, 0) %>%
    fill(Var3, Var4, .direction = "up")
#> # A tibble: 4 x 5
#>   Date        Avc1  Xrt2  Var3  Var4
#>   <date>     <int> <int> <int> <int>
#> 1 2017-04-04     0     0    22    25
#> 2 2017-04-04    10    18    22    25
#> 3 2017-04-04     8    12    16    20
#> 4 2017-04-04     5    10    13    18

df %>%
    mutate_at(vars(Var3, Var4), function(x) ifelse(x == 0, lead(x), x))
#> # A tibble: 4 x 5
#>   Date        Avc1  Xrt2  Var3  Var4
#>   <date>     <int> <int> <int> <int>
#> 1 2017-04-04     0     0    22    25
#> 2 2017-04-04    10    18    22    25
#> 3 2017-04-04     8    12    16    20
#> 4 2017-04-04     5    10    13    18

Created on 2018-05-06 by the reprex package (v0.2.0). reprex软件包 (v0.2.0)创建于2018-05-06。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM