Home > front end >  Problem to create a new columns in dataframe with lag
Problem to create a new columns in dataframe with lag

Time:10-23

I have a simple data_frame like under :

Blockquote

             timestamp   H_crops H_Tower
1     2021-01-01 00:00:00 0.1000000     2.1
2     2021-01-01 00:30:00 0.1000076      NA
3     2021-01-01 01:00:00 0.1000152      NA
4     2021-01-01 01:30:00 0.1000227      NA
5     2021-01-01 02:00:00 0.1000303      3
6     2021-01-01 02:30:00 0.1000379      NA
7     2021-01-01 03:00:00 0.1000455      NA
8     2021-01-01 03:30:00 0.1000530      NA
9     2021-01-01 04:00:00 0.1000606      10
10    2021-01-01 04:30:00 0.1000682      NA

Blockquote

I would like to get the following result :

                  timestamp   H_crops H_Tower diff
1     2021-01-01 00:00:00 0.1000000     2.1  2.1
2     2021-01-01 00:30:00 0.1000076      NA  2.1
3     2021-01-01 01:00:00 0.1000152      NA  2.1
4     2021-01-01 01:30:00 0.1000227      NA  2.1
5     2021-01-01 02:00:00 0.1000303       3   3
6     2021-01-01 02:30:00 0.1000379      NA   3
7     2021-01-01 03:00:00 0.1000455      NA   3
8     2021-01-01 03:30:00 0.1000530      NA   3
9     2021-01-01 04:00:00 0.1000606      10   10
10    2021-01-01 04:30:00 0.1000682      NA   10

I try this with dplyr:

group_by(timestamp)
  arrange(timestamp)
    mutate(data_glob3, diff= ifelse ( is.na(data_glob3[,"H_Tower"]),lag(H_Tower),data_glob3[,"H_Tower"]))

But the result is not good. it should use lag of diff but it is impossible . Could you help mes please ? Thanks a lot !

CodePudding user response:

It is not super clear what you are trying to do with the group_by and arrange functions there.

I am concretizing @Karthik S's suggestion since you are a newcomer and might not know how to apply it.

library(tidyverse)

mydat<- 
  read_table2(
  "timestamp H_crops H_Tower
2021-01-01 0.1000000     2.1
2021-01-01 0.1000076      NA
2021-01-01 0.1000152      NA
2021-01-01 0.1000227      NA
2021-01-01 0.1000303      3
2021-01-01 0.1000379      NA
2021-01-01 0.1000455      NA
2021-01-01 0.1000530      NA
2021-01-01 0.1000606      10
2021-01-01 0.1000682      NA
2021-01-01 0.1000530      NA
2021-01-02 0.1000606      NA
2021-01-02 0.1000682      3
2021-01-02 0.1000682      NA")


mydat %>% 
  group_by(timestamp) %>% 
  fill(H_Tower, .direction = "downup")

#> # A tibble: 14 × 3
#> # Groups:   timestamp [2]
#>    timestamp  H_crops H_Tower
#>    <date>       <dbl>   <dbl>
#>  1 2021-01-01   0.1       2.1
#>  2 2021-01-01   0.100     2.1
#>  3 2021-01-01   0.100     2.1
#>  4 2021-01-01   0.100     2.1
#>  5 2021-01-01   0.100     3  
#>  6 2021-01-01   0.100     3  
#>  7 2021-01-01   0.100     3  
#>  8 2021-01-01   0.100     3  
#>  9 2021-01-01   0.100    10  
#> 10 2021-01-01   0.100    10  
#> 11 2021-01-01   0.100    10  
#> 12 2021-01-02   0.100     3  
#> 13 2021-01-02   0.100     3  
#> 14 2021-01-02   0.100     3

Created on 2021-10-22 by the reprex package (v2.0.1)

CodePudding user response:

I am trying to do exactly what you do ! But I am a beginner with R, and I apply your solution in my dataframe without success...

the result is :

data_glob3 %>%
    group_by(timestamp) %>% 
    fill(H_Tower, .direction = "downup")
# A tibble: 12,721 x 3
# Groups:   timestamp [12,721]
   timestamp           H_crops H_Tower
   <dttm>                <dbl>   <dbl>
 1 2021-01-01 00:00:00   0.1       2.1
 2 2021-01-01 00:30:00   0.100    NA  
 3 2021-01-01 01:00:00   0.100    NA  
 4 2021-01-01 01:30:00   0.100    NA  
 5 2021-01-01 02:00:00   0.100    NA  
 6 2021-01-01 02:30:00   0.100    NA  
 7 2021-01-01 03:00:00   0.100    NA  
 8 2021-01-01 03:30:00   0.100    NA  
 9 2021-01-01 04:00:00   0.100    NA  
10 2021-01-01 04:30:00   0.100    NA  
# ... with 12,711 more rows
  • Related