Advertisement
hohiyan

ptt-r-20220721

Jul 21st, 2022
686
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 0.89 KB | None | 0 0
  1. library(data.table)
  2.  
  3. df <- data.table(hour = 1:24,
  4.                  RH = c(sample(50:80, 20, replace = T), rep(NA, 4)))
  5.  
  6. df$RH <- sample(df$RH)
  7.  
  8. df[, RH03 := shift(RH, n=3L, fill=0, type="lag")]
  9.  
  10. df
  11. #>      hour    RH  RH03
  12. #>     <int> <int> <int>
  13. #>  1:     1    NA     0
  14. #>  2:     2    50     0
  15. #>  3:     3    71     0
  16. #>  4:     4    68    NA
  17. #>  5:     5    54    50
  18. #>  6:     6    76    71
  19. #>  7:     7    NA    68
  20. #>  8:     8    66    54
  21. #>  9:     9    64    76
  22. #> 10:    10    63    NA
  23. #> 11:    11    51    66
  24. #> 12:    12    69    64
  25. #> 13:    13    80    63
  26. #> 14:    14    70    51
  27. #> 15:    15    58    69
  28. #> 16:    16    60    80
  29. #> 17:    17    74    70
  30. #> 18:    18    NA    58
  31. #> 19:    19    65    60
  32. #> 20:    20    79    74
  33. #> 21:    21    64    NA
  34. #> 22:    22    62    65
  35. #> 23:    23    NA    79
  36. #> 24:    24    71    64
  37. #>      hour    RH  RH03
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement