R中的list()和c()有啥区别,以及如何在函数中传递列表元素

Posted

技术标签:

【中文标题】R中的list()和c()有啥区别,以及如何在函数中传递列表元素【英文标题】:what is the differences among list() and c() in R, and how to pass list elements in a functionR中的list()和c()有什么区别,以及如何在函数中传递列表元素 【发布时间】:2022-01-20 15:28:19 【问题描述】:

我有多个 data.frames,例如:

DF1[1:3, ]

                                          Category Subcategory                Subsystem                                                                     Role NameDF1
1 Cofactors, Vitamins, Prosthetic Groups, Pigments      Biotin Biotin synthesis cluster           Competence protein F homolog, phosphoribosyltransferase domain          1
2 Cofactors, Vitamins, Prosthetic Groups, Pigments      Biotin Biotin synthesis cluster                                                  Biotin operon repressor          1
3 Cofactors, Vitamins, Prosthetic Groups, Pigments      Biotin Biotin synthesis cluster Adenosylmethionine-8-amino-7-oxononanoate aminotransferase (EC 2.6.1.62)          1


DF2[1:3, ]
                                          Category Subcategory                Subsystem                                                                     Role NameDF2
1 Cofactors, Vitamins, Prosthetic Groups, Pigments      Biotin Biotin synthesis cluster           Competence protein F homolog, phosphoribosyltransferase domain          1
2 Cofactors, Vitamins, Prosthetic Groups, Pigments      Biotin Biotin synthesis cluster                                                  Biotin operon repressor          1
3 Cofactors, Vitamins, Prosthetic Groups, Pigments      Biotin Biotin synthesis cluster Adenosylmethionine-8-amino-7-oxononanoate aminotransferase (EC 2.6.1.62)          1

DF3[1:3, ]
                                          Category Subcategory                Subsystem                                                                     Role NameDF3
1 Cofactors, Vitamins, Prosthetic Groups, Pigments      Biotin Biotin synthesis cluster           Competence protein F homolog, phosphoribosyltransferase domain          1
2 Cofactors, Vitamins, Prosthetic Groups, Pigments      Biotin Biotin synthesis cluster                                                  Biotin operon repressor          1
3 Cofactors, Vitamins, Prosthetic Groups, Pigments      Biotin Biotin synthesis cluster Adenosylmethionine-8-amino-7-oxononanoate aminotransferase (EC 2.6.1.62)          1

所以,当我绑定制作单个 data.frame 时,我通常会这样做:

myDF <- list(DF1, DF2, DF3) %>% purrr::reduce(full_join, by =c("Category", "Subcategory", "Subsystem", "Role")) 

结果是

myDF[1:3, 1:7]

                                          Category Subcategory                Subsystem                                                                     Role      NameDF1    NameDF2    NameDF3
1 Cofactors, Vitamins, Prosthetic Groups, Pigments      Biotin Biotin synthesis cluster           Competence protein F homolog, phosphoribosyltransferase domain          1          1          1
2 Cofactors, Vitamins, Prosthetic Groups, Pigments      Biotin Biotin synthesis cluster                                                  Biotin operon repressor          1          1          1
3 Cofactors, Vitamins, Prosthetic Groups, Pigments      Biotin Biotin synthesis cluster Adenosylmethionine-8-amino-7-oxononanoate aminotransferase (EC 2.6.1.62)          1          1          1

到这里为止一切正常,我的问题是当我尝试创建一个函数并使用 c() 而不是 list() 时

myFunction <- function(Files=NULL)
        # the function is longer, but this part is my problem !! 
        require(dplyr)
        require(purrr)
        l <- list(Files)
        myDF <- l %>% reduce(full_join, by =c("Category", "Subcategory", "Subsystem", "Role"))
        return(myDF)

    

myFunction(Files=c(DF1, DF2, DF2))

它给了我一个列表,但我想要一个 data.frame,所以我删除了 l

myFunction <- function(Files=NULL)
        require(dplyr)
        require(purrr)
        myDF <- Files %>% reduce(full_join, by =c("Category", "Subcategory", "Subsystem", "Role"))
        return(myDF)

    

然后

l <- list(DF1, DF2, DF2)

myFunction(Files=l )

它给了我正确的格式:

myDF[1:3, ]

                                          Category Subcategory                Subsystem                                                                     Role      NameDF1    NameDF2    NameDF3
1 Cofactors, Vitamins, Prosthetic Groups, Pigments      Biotin Biotin synthesis cluster           Competence protein F homolog, phosphoribosyltransferase domain          1          1          1
2 Cofactors, Vitamins, Prosthetic Groups, Pigments      Biotin Biotin synthesis cluster                                                  Biotin operon repressor          1          1          1
3 Cofactors, Vitamins, Prosthetic Groups, Pigments      Biotin Biotin synthesis cluster Adenosylmethionine-8-amino-7-oxononanoate aminotransferase (EC 2.6.1.62)          1          1          1

我的问题是,如何使用 c() 在参数函数中添加所有 data.frames(R 函数中最常见的方法是什么)?并且不要在函数之外使用 list() !!!

我想要类似的东西:

myFunction(Files=c(DF1,DF2,DF3,DFn))

我不想在函数之外使用 list()

l <- list(DF1,DF2,DF3,DFn)
myFunction(Files=l)

谢谢

【问题讨论】:

【参考方案1】:

list(DF1, DF2, DF3) 传递给您的函数并没有错,这可能是最好的选择。如果还是不喜欢,也可以试试以下方法:

myFunction = function(...) 
  require(dplyr)
  require(purrr)
  dfs = list(...)
  reduce(
    dfs, full_join, 
    by =c("Category", "Subcategory", "Subsystem", "Role")
  )


myFunction(DF1, DF2, DF3)

解释原始代码的问题:当您在示例中调用reduce() 时,您需要传递数据帧列表,即list(DF1, DF2, DF3)。您的问题是 list(c(DF1, DF2, DF3))(这是您的原始函数创建的)和 list(DF1, DF2, DF3) 完全不同。

我建议您在控制台中运行这两个表达式以便更好地理解。由于数据框是一个列表,c(DF1, DF2, DF3) 创建一个列表,其中包含每个数据框的列作为元素。您真正想要的是一个包含三个单独列表(它们是数据框)的列表。

【讨论】:

以上是关于R中的list()和c()有啥区别,以及如何在函数中传递列表元素的主要内容,如果未能解决你的问题,请参考以下文章

fopen中的r和rb有啥区别

gc() 和 rm() 有啥区别

c语言中printf与p r i n t f有啥区别

在 Dart 中,List.from 和 .of 以及 Map.from 和 .of 有啥区别?

c语言中函数定义和声明有啥区别

R中的()和[]有啥区别[关闭]