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
334 views
in Technique[技术] by (71.8m points)

r - Is there anyway to use lag and lead functions together from dplyr

I have several *.txt files that look like

ID BP
Id1 A
Id2 A
Id3 T
Id4 C
Id5 A
Id6 T
Id7 A
Id8 T

I want for every ID previous 4 characters from BP and the next 2 characters of BP

something like:

Id5 A CTAA TA 
Id6 T TACT AT 

I am trying to achieve this by using lag and lead functions from dplyr but not able to get the output as expected.

question from:https://stackoverflow.com/questions/65839033/is-there-anyway-to-use-lag-and-lead-functions-together-from-dplyr

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

1 Reply

0 votes
by (71.8m points)

Instead of using lead and lag you can use rolling operations which can be adapted easily if your window size increases/decreases.

library(dplyr)
library(zoo)

df %>%
  mutate(result1 = lag(rollapplyr(BP, 4, function(x) 
                       paste0(rev(x), collapse = ''), fill = NA)), 
         result2 = rollapply(BP, 2, align = 'left', function(x) 
                       paste0(rev(x), collapse = ''), fill = NA))

#   ID BP result1 result2
#1 Id1  A    <NA>      AA
#2 Id2  A    <NA>      TA
#3 Id3  T    <NA>      CT
#4 Id4  C    <NA>      AC
#5 Id5  A    CTAA      TA
#6 Id6  T    ACTA      AT
#7 Id7  A    TACT      TA
#8 Id8  T    ATAC    <NA>

Suggestion by @G. Grothendieck avoids the above hacky way with rev and lag.

df %>% 
  mutate(result11 = rollapply(BP,list(-(1:4)), paste, collapse = '', fill = NA), 
         result2 = rollapply(BP, list(1:2), paste, collapse = '', fill = NA))

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

...