gpt4 book ai didi

r - 在 R 中与 biomart 循环

转载 作者:行者123 更新时间:2023-12-04 15:16:44 25 4
gpt4 key购买 nike

我有一个基于许多文件创建的数据集列表。


list.function <- function() {

sample1 <- data.frame(ensembl.id = c("ENSG00000000005.6", "ENSG00000000003.15", "ENSG00000000419.13", "ENSG00000000457.14", "ENSG00000000460.17"), counts = c(4, 5, 6, 1, 1))
sample2 <- data.frame(ensembl.id = c("ENSG00000000005.6", "ENSG00000000003.15", "ENSG00000000419.13", "ENSG00000000457.14", "ENSG00000000460.17"), counts = c(4, 5, 6, 1, 1))
sample3 <- data.frame(ensembl.id = c("ENSG00000000005.6", "ENSG00000000003.15", "ENSG00000000419.13", "ENSG00000000457.14", "ENSG00000000460.17"), counts = c(4, 5, 6, 1, 1))
sample4 <- data.frame(ensembl.id = c("ENSG00000000005.6", "ENSG00000000003.15", "ENSG00000000419.13", "ENSG00000000457.14", "ENSG00000000460.17"), counts = c(4, 5, 6, 1, 1))

sapply(paste('sample', seq(1,4,1), sep=''), get, environment(), simplify = FALSE)
}

my.list3 <- list.function()
my.list3



library("biomaRt")
grch38 <- useMart("ensembl",dataset="hsapiens_gene_ensembl")

我正在尝试自动执行此操作:


my.list4 = lapply(my.list3, function(x){

atributos = getBM(attributes = c("ensembl_gene_id_version", "external_gene_name", "chromosome_name", "gene_biotype", "entrezgene_description"),
filters = "ensembl_gene_id_version",
values = x$ensembl.id,
mart = grch38)


atributos_unique = atributos %>% distinct(ensembl_gene_id_version, .keep_all = TRUE)


merged = merge(x, atributos_unique, by.x="ensembl.id", by.y="ensembl_gene_id_version" )


merged$gene_biotype = as.factor(merged$gene_biotype)
})

正确使用所有数据集,但输出不正确!

我需要“合并”的最终输出对于我的“my.list3”列表中的每个数据集都是唯一的,并且与原始数据集同名

有什么想法吗?

最佳答案

您没有在函数调用中返回数据框。

library(biomaRt)
library(tidyverse)

grch38 = useMart("ensembl", dataset="hsapiens_gene_ensembl")

my.list4 = lapply(my.list3, function(x){
atributos = getBM(attributes = c("ensembl_gene_id_version",
"external_gene_name",
"chromosome_name",
"gene_biotype",
"entrezgene_description"),
filters = "ensembl_gene_id_version",
values = x$ensembl.id,
mart = grch38)


atributos_unique = atributos %>%
distinct(ensembl_gene_id_version, .keep_all = TRUE)


merged = merge(x,
atributos_unique,
by.x="ensembl.id",
by.y="ensembl_gene_id_version" )


merged$gene_biotype = as.factor(merged$gene_biotype)
return(merged) #or just merged
})

在函数调用末尾添加return(merged)

关于r - 在 R 中与 biomart 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64199744/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com