I want to add a column of gwas table that indicate which gene it is based on the start and end position of the gene. How to do this in dplyr?
> gwas
# A tibble: 1,220,764 x 13
CHROM POS ID REF ALT A1 TEST OBS_CT BETA SE T_STAT P ERRCODE
<int> <int> <chr> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <chr>
1 1 10511 rs534229142 A G A ADD 461822 0.0825 0.0439 1.88 0.0603 .
2 1 15031 rs568188357 A G A ADD 461225 0.0410 0.0803 0.511 0.610 .
3 1 15245 rs576044687 T C T ADD 461842 0.0369 0.0444 0.831 0.406 .
4 1 46285 1:46285_ATAT_A A ATAT A ADD 461698 0.0205 0.0307 0.666 0.505 .
5 1 49315 rs567788405 A T A ADD 460193 0.0576 0.0552 1.04 0.297 .
6 1 49318 rs536836601 G A G ADD 460428 0.0297 0.0420 0.708 0.479 .
7 1 49343 rs553572247 C T C ADD 461927 -0.00305 0.0360 -0.0846 0.933 .
8 1 51047 rs559500163 T A T ADD 462466 0.0459 0.0402 1.14 0.254 .
9 1 51049 rs528344458 C A C ADD 462466 0.0459 0.0402 1.14 0.254 .
10 1 51050 rs551668143 T A T ADD 462466 0.0459 0.0402 1.14 0.254 .
# … with 1,220,754 more rows
> glist
# A tibble: 2,566 x 4
chr start end gene_name
<int> <int> <int> <chr>
1 1 33772366 33786699 A3GALT2
2 1 12776117 12788726 AADACL3
3 1 12704565 12727097 AADACL4
4 1 94458393 94586705 ABCA4
5 1 229652328 229694442 ABCB10
6 1 94883932 94984219 ABCD3
7 1 179068461 179198819 ABL2
8 1 76190031 76229363 ACADM
9 1 1227763 1243269 ACAP3
10 1 226332379 226374423 ACBD3
# … with 2,556 more rows
I defined function
find_gene_name <- function(POS) {
interval <- glist %>% filter(start<=POS, POS<=end)
name <- interval$gene_name
if(length(name)) {
return(NA)
} else {
return(name)
}
}
But got the error when using rowwise.
> gwas_annotated <- gwas %>%
+ rowwise() %>%
+ mutate(gene_name=find_gene_name(POS))
Error: Problem with `mutate()` input `gene_name`.
x Input `gene_name` can't be recycled to size 1.
ℹ Input `gene_name` is `find_gene_name(POS)`.
ℹ Input `gene_name` must be size 1, not 0.
ℹ Did you mean: `gene_name = list(find_gene_name(POS))` ?
ℹ The error occurred in row 1.
Run `rlang::last_error()` to see where the error occurred.