在同一新列中同时领先和滞后多个值

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

这是我的数据:

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           NA
2023-11-01   12           12
2023-11-01   12           13
2023-11-01   12           12
2023-11-02   13           NA
2023-11-02   13           12
2023-11-02   13           13
2023-11-02   13           12
2023-11-02   13           11
2023-11-03   12           12
2023-11-03   12           13
2023-11-03   12           12
2023-11-03   12           11
2023-11-03   12           15
2023-11-04   11           13
2023-11-04   11           12
2023-11-04   11           11
2023-11-04   11           15
2023-11-04   11           NA
2023-11-05   15           12
2023-11-05   15           11
2023-11-05   15           15
2023-11-05   15           NA
2023-11-05   15           NA

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

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

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

一个

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

0
投票

在 Base R 中,使用以下内容:

n <- 2
m <- length(df$tmax)
x <- c(rep(NA, n), df$tmax, rep(NA, n))
data.frame(df[rep(seq(m), each=m),], 
                     tmax_climate = x[sequence(rep(m,m), seq(m))],
                     row.names = NULL)

         date tmax tmax_climate
1  2023-11-01   12           NA
2  2023-11-01   12           NA
3  2023-11-01   12           12
4  2023-11-01   12           13
5  2023-11-01   12           12
6  2023-11-02   13           NA
7  2023-11-02   13           12
8  2023-11-02   13           13
9  2023-11-02   13           12
10 2023-11-02   13           11
11 2023-11-03   12           12
12 2023-11-03   12           13
13 2023-11-03   12           12
14 2023-11-03   12           11
15 2023-11-03   12           15
16 2023-11-04   11           13
17 2023-11-04   11           12
18 2023-11-04   11           11
19 2023-11-04   11           15
20 2023-11-04   11           NA
21 2023-11-05   15           12
22 2023-11-05   15           11
23 2023-11-05   15           15
24 2023-11-05   15           NA
25 2023-11-05   15           NA
© www.soinside.com 2019 - 2024. All rights reserved.