如何在r循环中运行函数
我有一个数据框和一组基因组坐标。我希望使用如何在r循环中运行函数,r,loops,apply,R,Loops,Apply,我有一个数据框和一组基因组坐标。我希望使用nearest.gene()查找这些坐标周围的基因,它一次打印一个结果。我一直在努力在循环中运行函数: 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
nearest.gene()
查找这些坐标周围的基因,它一次打印一个结果。我一直在努力在循环中运行函数:
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"
它将覆盖接下来九个坐标的第一个输出。
是否有更好的方法运行此函数?我想您可以尝试以下代码,您应该将
x
传递到函数nearest.gene()
在你的
apply
函数中,你没有将x
传递到你的函数nearest.gene()
谢谢你指出这一点。它解决了问题吗?希望它能很好地工作……是的,它做到了。再次感谢你的帮助,太好了!如果你认为我的回答有帮助,请随意投票/接受。谢谢:)
apply(gene_lst, 1, function (x) nearest.gene(chr=x["Chr"], pos=x["Pos"]))