Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
rbind函数中的多个数据帧_R_Get_Rbind_Do.call - Fatal编程技术网

rbind函数中的多个数据帧

rbind函数中的多个数据帧,r,get,rbind,do.call,R,Get,Rbind,Do.call,我在下面找到了这一行代码,它在一个函数之外起到了魔力的作用,可以识别数据帧列表并使用rbind连接它们 mylist<-ls(pattern='leg_') mleg <- do.call(rbind, lapply(mylist, get)) 有什么帮助吗 更新 我找到了另一种方法来实现我所需要的,但我确信这是低效的,尽管我的数据帧列表最多有4个 for(i in 1:(length(blg_idx))){

我在下面找到了这一行代码,它在一个函数之外起到了魔力的作用,可以识别数据帧列表并使用rbind连接它们

    mylist<-ls(pattern='leg_')
    mleg <- do.call(rbind, lapply(mylist, get))
有什么帮助吗

更新

我找到了另一种方法来实现我所需要的,但我确信这是低效的,尽管我的数据帧列表最多有4个

            for(i in 1:(length(blg_idx))){
                assign(paste(deparse(substitute(leg_)),i,sep=''),l_merge(get(paste(deparse(substitute(blg)),i,sep='')),get(paste(deparse(substitute(bsg)),i,sep=''))))
            }

            mylist<-ls(pattern='leg_')

            #return(mylist)
            #mlegleg <- rbind(leg_1,leg_2) # this works 
            # mleg <- do.call(rbind, lapply(mylist, get))

            mleg <- leg_1

            for(i in 2:(length(blg_idx))){
                mleg <- rbind(leg,get(paste(deparse(substitute(leg_)),i,sep='')))
            }   

            return(mleg)
            } #end read_leg

您没有可重复的示例来测试此解决方案,但请查看
get
的帮助页面,然后尝试以下操作:

mleg <- do.call(rbind, lapply(mylist, get, envir = globalenv() ))

mleg上面的答案包含了你问题的答案:
envir=globalenv()
我花了一段时间才意识到R将为每个函数创建一个私有环境。在这个私有环境中,其他变量不存在。也就是说,除非您使用
envir
参数告诉函数查看全局环境

这里有一个函数,它应该以字符串作为输入,然后识别全局环境中所有变量(例如数据帧),这些变量的名称中包含该字符串。然后它将尝试绑定这些变量(数据帧)


如果所有变量都是具有相同列名的数据帧,那么它应该返回一个绑定的数据帧
myBindedDF回复很晚,但我刚刚遇到了一个与更新2类似的问题,其中“object'pur_1'未找到”

如果您希望在具有未知数量的以“pur_”开头的数据帧时在函数中使用以下内容,例如:

mylist <- ls(pattern='pur_')
mleg <- do.call(rbind, lapply(mylist, get))

mylist感谢您的建议,我不得不调整您的解决方案,如下所示。我无法更新我的评论,但这是我通过反复试验为我编写的代码。调用(rbind,mget(paste0(“pur”,1:2),envir=as.environment(-1)))
    read_date <- function(x){
    pur_1 <- data.frame(sku=859, X = sample(1:10),Y = sample(c("yes", "no"), 10, replace = TRUE))
    pur_2 <- data.frame(sku=859, X = sample(11:20),Y = sample(c("yes", "no","na"), 10, replace = TRUE))

    mylist<-ls(pattern='pur_')

    pur_final <- do.call(rbind, lapply(mylist, get))
    #fancier version that I want to achieve is below
    #assign(paste('pur_',eval(pur_1$sku[1]),sep=''),do.call(rbind, lapply(mylist, get)))
    return(pur_final)
}

read_date()
pur_final <- do.call(rbind, mget(paste0("pur_", 1:2),envir = as.environment(-1)))
    >   read_date()
             sku  X   Y
    pur_1.1  859  8 yes
    pur_1.2  859  4  no
    pur_1.3  859  3 yes
    ....
    pur_2.8  859 14  na
    pur_2.9  859 13  na
    pur_2.10 859 19  no
    >  
mleg <- do.call(rbind, lapply(mylist, get, envir = globalenv() ))
bindCompatibleTables <- function(x){
  if(is.character(x)){
  mylist <-   grep(x, ls(pos = 1), value=T)
  mergedDF <- do.call(rbind,  mget(mylist,envir = as.environment(1)))

  return(bindedDF)
  } else {
    stop("Input is not a character string")
  }
}
mylist <- ls(pattern='pur_')
mleg <- do.call(rbind, lapply(mylist, get))
mylist <- ls(pattern='pur_')
mleg <- do.call(rbind, lapply(mylist, get, env=environment()))