Right now I have two data tables in the form:
library(dplyr)
> lengths
chrom p q
1 2560 78934
2 1346 9087
3 678 7809
4 5467 1253
...
> my_data
mut pos chr arm
A 567 2 p
B 6890 2 q
C 978 3 q
D 4689 4 p
...
I want to add another column my_data$chrom.pos, that is conditionally adding values from lengths$q to the my_data$pos values IF my_data$arm is 'p', but not 'q'.
What I have thus far:
my_data %>% mutate(chrom.pos =
case_when (arm == "q" ~ pos,
arm == "p" ~ pos + lengths[["q"]][chr])
)
While this handles the 'q' condition just fine, any case that is 'p' comes out as NA because I'm not getting the linking quite right.
> my_data
mut pos chr arm chrom.pos
A 567 2 p NA # Looking for 9087 + 567 = 9654
B 6890 2 q 6890
C 978 3 q 978
D 4689 4 p NA # Looking for 1253 + 4689 = 5942