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

Viewed 141

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.

1 Answers

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))
Related