AST
AST

Reputation: 57

How to run a function in a r loop

I have a dataframe with a set of genomic co-ordinates. I wish to find genes around those co-ordinates using nearest.gene() which print result one at a time. I have been struggling to run the function in a loop:

apply(gene_lst, 1, function (x) nearest.gene(chr=gene_lst$Chr, pos=gene_lst$Pos))
      1       2       3       4       5       6       7       8       9      10 
"ACBD3" "ACBD3" "ACBD3" "ACBD3" "ACBD3" "ACBD3" "ACBD3" "ACBD3" "ACBD3" "ACBD3"

It is overwriting the first output for the next nine co-ordinates. Is there a better way to run this function?

Upvotes: 0

Views: 63

Answers (1)

ThomasIsCoding
ThomasIsCoding

Reputation: 102770

I guess you can have a try with the following code, where you should pass x into your function nearest.gene()

apply(gene_lst, 1, function (x) nearest.gene(chr=x["Chr"], pos=x["Pos"]))

Upvotes: 1

Related Questions