超前值和滞后值一起出现在同一新列中

问题描述 投票:0回答:1

这是我的数据:

structure(list(date = structure(c(19662, 19663, 19664, 19665, 
19666), class = "Date"), tmax = c(12, 13, 12, 11, 15)), class = "data.frame", row.names = c(NA, 
-5L))

        date tmax
2023-11-01   12
2023-11-02   13
2023-11-03   12
2023-11-04   11
2023-11-05   15

想要实现的目标如下:

        date tmax tmax_climate
2023-11-01   12           NA
2023-11-01   12           12
2023-11-01   12           13
2023-11-02   13           12
2023-11-02   13           13
2023-11-02   13           12
2023-11-03   12           13
2023-11-03   12           12
2023-11-03   12           11
2023-11-04   11           12
2023-11-04   11           11
2023-11-04   11           15
2023-11-05   15           11
2023-11-05   15           15
2023-11-05   15           NA

基本上我想要的是获得前一天的

tmax
和第二天的
tmax
。我已经尝试过
rollapply()
lead()
lag()
,但到目前为止还没有运气。我最好坚持
dplyr
tmax_climate
中的顺序并不重要(不需要如所需输出那样为
tmax
-1、
tmax
tmax
+1)

r dplyr lag lead rollapply
1个回答
0
投票

一个

dplyr
选项:

library(dplyr)
library(tidyr)

df %>%
  mutate(lag1 = lag(tmax),
         lead1 = lead(tmax),
         current = tmax) %>%
  pivot_longer(cols = c(lag1, lead1, current),
               values_to = 'tmax_climate') %>%
  select(-name)
#> # A tibble: 15 × 3
#>    date        tmax tmax_climate
#>    <date>     <dbl>        <dbl>
#>  1 2023-11-01    12           NA
#>  2 2023-11-01    12           13
#>  3 2023-11-01    12           12
#>  4 2023-11-02    13           12
#>  5 2023-11-02    13           12
#>  6 2023-11-02    13           13
#>  7 2023-11-03    12           13
#>  8 2023-11-03    12           11
#>  9 2023-11-03    12           12
#> 10 2023-11-04    11           12
#> 11 2023-11-04    11           15
#> 12 2023-11-04    11           11
#> 13 2023-11-05    15           11
#> 14 2023-11-05    15           NA
#> 15 2023-11-05    15           15
© www.soinside.com 2019 - 2024. All rights reserved.