【问题标题】:applying functions on all data frames which are element of a list and then clubbing modified data frame在作为列表元素的所有数据帧上应用函数,然后合并修改后的数据帧
【发布时间】:2015-02-11 13:03:45
【问题描述】:

mylistlist 对象。
mylist[[1]]mylist[[1000]] 是 1000 data.frames
每个data.frame 的列数相同。

定义了函数decomp,它可以通过添加一列来修改data.frame(输入:data.frame,输出:data.frame
然后我们必须修改所有data.frames,然后我们必须将它们合并在一起。我们该怎么做?

【问题讨论】:

  • 你所说的“俱乐部在一起”是什么意思?您应该提供一个可重现的示例,其中包含您拥有的输入和所需的输出。否则,很难帮你……

标签: r function merge dataframe


【解决方案1】:

我不知道我是否理解你的问题。

#create dataframes
a<-data.frame(col1=c(1,2,3), col2=c(4,5,6) )
b<-data.frame(col3=c(6,7,8), col4=c(9,10,11) )
c<-data.frame(col5=c(12,13,14), col6=c(15,16,17) )

#create the list with dataframes
mylist<-list()
mylist[[1]]<-a
mylist[[2]]<-b
mylist[[3]]<-c


#create funcion that add a column
decomp<-function(df,col) { return(data.frame(df,newcol=col)) }

#column to add
column<-c('x','y','z')

#add a column to each data.frame
mylist<-lapply(mylist,decomp,col=column)

#to merge the tables
    data<- mylist[[1]]
for (i in 2:length(mylist)) {
data<-merge(data,mylist[[i]],all=T,by="newcol")
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-12
    • 2020-11-02
    • 2020-10-26
    • 1970-01-01
    • 2020-06-06
    • 1970-01-01
    • 2020-10-30
    • 2018-10-03
    相关资源
    最近更新 更多