Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
377 views
in Technique[技术] by (71.8m points)

rows - Use lag function in R to call multiple values

I made a similar question : If condition is met, go 6 rows above and select the value in R and I received a very usefyl answer.

Now I am trying to call instead of 1 value as in the previous topic, multiple rows instead.

Sample data:

    md2 <- structure(list(Hdwy = c(45.01, 45.03, 449, 44.46, 43.63, 425, 
41.36, 40.53, 40.1, 39.97, 39.98, 40, 40, 40, 40, 41.36, 40.53, 
40.1, 40, 40), L_ID = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 1)), class = "data.frame", row.names = c(NA, 
-20L))

I used :

library(dplyr)
spc <- md2 %>%
         mutate(Lag = lag(Hdwy, (1:6)) %>%
         filter(L_ID==1) %>%
         pull(Lag)
spc

Expecting to see:

> spc 44.46, 43.63, 425, 
>     41.36, 40.53, 40.1

But I get the error:

Error: Problem with mutate() input Lag. x n must be a nonnegative integer scalar, not an integer vector of length 200. i Input Lag is `lag(Hdwy, (1:6))

Any ideas? Should I use a different function instead?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

lag can accept only 1 number, you can try a rolling operation instead.

library(dplyr)

md2 %>% 
  mutate(Lag = lead(zoo::rollapply(L_ID == 1, 6, any, fill = NA, align = 'left'))) %>%
  filter(Lag) %>%
  pull(Hdwy)

#[1]  44.46  43.63 425.00  41.36  40.53  40.10  40.00

I do get another value 40 corresponding to the last 1.

If you want to get all the 6 values from the each 1 you can try this base R option.

inds <- which(md2$L_ID == 1)
md2$Hdwy[unique(sort(c(sapply(inds, `-`, 1:6))))]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...