将data.frames列表转换为R中的单个数据帧

时间:2015-03-10 13:18:16

标签: r list dataframe

我使用XLConnect包将一堆电子表格导入R作为数据帧列表,并且:

    sheet_names <- getSheets(wb)
names(sheet_names) <- sheet_names
sheet_list <- lapply(sheet_names, function(.sheet){
  readWorksheet(object=wb, .sheet)})

我现在正试图将这些数据帧(每个134个观察8个变量)组合成一个数据帧,以便我可以进行一些进一步的分析。我发现这行代码让我有了一些方法:

sh_combined <- do.call("cbind", sheet_list)

然而,这产生了一个数据帧,该数据帧由203个变量组成,其中8个变量中的每一个都已被复制。理想情况下,我的组合数据框将有一个变量'name',它是每个原始数据帧的名称 - n.b.在这种情况下,29个数据框中的每一个都代表对20个不同组织回答的问卷的答复。

我并不习惯使用列表,因此无法找到实现此目的的便捷方法。另一个问题是数据在首次捕获(格式化为excel)时结构严重,因此并不完全“整洁”。但是,各个电子表格都具有一致的行名和列名。

整个清单很大,但结构如下:

List of 29


$ Alliance Youth Group :'data.frame':   134 obs. of  7 variables:
  ..$ Sector         : chr [1:134] "Customer Segements" NA NA NA ...
  ..$ Subject        : chr [1:134] "Waste Generators" NA NA NA ...
  ..$ Variable       : chr [1:134] "Residential (Household)" "Residential (Apartment)" "Commercial (Dukas)" "Commercial (Bandas)" ...
  ..$ Yes.No.NA      : chr [1:134] "192" "8" "20" "5" ...
  ..$ Revenue.Streams: num [1:134] 9600 3600 4800 250 NA NA 900 1000 1200 300 ...
  ..$ Cost.Structure : num [1:134] NA NA NA NA NA NA NA NA NA NA ...
  ..$ Notes          : chr [1:134] NA NA NA NA ...
 $ Bidii Kweli          :'data.frame':  134 obs. of  7 variables:
  ..$ Sector         : chr [1:134] "Customer Segements" NA NA NA ...
  ..$ Subject        : chr [1:134] "Waste Generators" NA NA NA ...
  ..$ Variable       : chr [1:134] "Residential (Household)" "Residential (Apartment)" "Commercial (Dukas)" "Commercial (Bandas)" ...
  ..$ Yes.No.NA      : chr [1:134] "300" "0" "12" "5" ...
  ..$ Revenue.Streams: num [1:134] 60000 NA 960 600 NA NA 160 NA 240 NA ...
  ..$ Cost.Stucture  : num [1:134] NA NA NA NA NA NA NA NA NA NA ...
  ..$ Notes          : chr [1:134] NA NA NA NA ...
 $ Bidiika              :'data.frame':  134 obs. of  7 variables:
  ..$ Sector         : chr [1:134] "Customer Segements" NA NA NA ...
  ..$ Subject        : chr [1:134] "Waste Generators" NA NA NA ...
  ..$ Variable       : chr [1:134] "Residential (Household)" "Residential (Apartment)" "Commercial (Dukas)" "Commercial (Bandas)" ...
  ..$ Yes.No.NA      : chr [1:134] "82" "N/A" "12" "1" ...
  ..$ Revenue.Streams: num [1:134] 4592 NA 1800 400 NA ...
  ..$ Cost.Stucture  : num [1:134] NA NA NA NA NA NA NA NA NA NA ...
  ..$ Notes          : chr [1:134] NA NA NA NA ...
 $ BigShip              :'data.frame':  134 obs. of  7 variables:
  ..$ Sector         : chr [1:134] "Customer Segements" NA NA NA ...
  ..$ Subject        : chr [1:134] "Waste Generators" NA NA NA ...
  ..$ Variable       : chr [1:134] "Residential (Household)" "Residential (Apartment)" "Commercial (Dukas)" "Commercial (Bandas)" ...
  ..$ Yes.No.NA      : chr [1:134] "100" "104" "30" "0" ...
  ..$ Revenue.Streams: num [1:134] 30000 31200 9000 NA 3500 NA 2100 17500 17500 NA ...
  ..$ Cost.Structure : num [1:134] NA NA NA NA NA NA NA NA NA NA ...
  ..$ Notes          : chr [1:134] NA NA NA NA ...
 $ Bokole               :'data.frame':  134 obs. of  7 variables:
  ..$ Sector         : chr [1:134] "Customer Segements" NA NA NA ...
  ..$ Subject        : chr [1:134] "Waste Generators" NA NA NA ...
  ..$ Variable       : chr [1:134] "Residential (Household)" "Residential (Apartment)" "Commercial (Dukas)" "Commercial (Bandas)" ...
  ..$ Yes.No.NA      : chr [1:134] "50" "N/A" "N/A" "N/A" ...
  ..$ Revenue.Streams: num [1:134] 10000 NA NA NA NA NA NA NA 200 NA ...
  ..$ Cost.Structure : num [1:134] NA NA NA NA NA NA NA NA NA NA ...
  ..$ Notes          : chr [1:134] NA NA NA NA ...
 $ Brilliant Minds      :'data.frame':  134 obs. of  7 variables:
  ..$ Sector         : chr [1:134] "Customer Segements" NA NA NA ...
  ..$ Subject        : chr [1:134] "Waste Generators" NA NA NA ...
  ..$ Variable       : chr [1:134] "Residential (Household)" "Residential (Apartment)" "Commercial (Dukas)" "Commercial (Bandas)" ...
  ..$ Yes.No.NA      : chr [1:134] "20" "N/A" "5" "N/A" ...
  ..$ Revenue.Streams: num [1:134] 6000 NA 250 NA NA NA NA NA NA NA ...
  ..$ Cost.Stucture  : num [1:134] NA NA NA NA NA NA NA NA NA NA ...
  ..$ Notes          : chr [1:134] NA NA NA NA ...
 $ Changing Ambassador  :'data.frame':  134 obs. of  7 variables:
  ..$ Sector         : chr [1:134] "Customer Segements" NA NA NA ...
  ..$ Subject        : chr [1:134] "Waste Generators" NA NA NA ...
  ..$ Variable       : chr [1:134] "Residential (Household)" "Residential (Apartment)" "Commercial (Dukas)" "Commercial (Bandas)" ...
  ..$ Yes.No.NA      : chr [1:134] "300" "4" "0" "0" ...
  ..$ Revenue.Streams: num [1:134] 75000 600 0 0 NA NA NA NA NA NA ...
  ..$ Cost.Structure : num [1:134] NA NA NA NA NA NA NA NA NA NA ...
  ..$ Notes          : chr [1:134] NA NA NA NA ...
 $ Chenda Investments   :'data.frame':  134 obs. of  7 variables:
  ..$ Sector         : chr [1:134] "Customer Segements" NA NA NA ...
  ..$ Subject        : chr [1:134] "Waste Generators" NA NA NA ...
  ..$ Variable       : chr [1:134] "Residential (Household)" "Residential (Apartment)" "Commercial (Dukas)" "Commercial (Bandas)" ...
  ..$ Yes.No.NA      : chr [1:134] "No" "15" "20" "No" ...
  ..$ Revenue.Streams: num [1:134] NA 27000 60000 NA NA NA NA NA NA NA ...
  ..$ Cost.Structure : num [1:134] NA NA NA NA NA NA NA NA NA NA ...
  ..$ Notes          : chr [1:134] NA NA NA NA ...
 $ Customer Segments    :'data.frame':  134 obs. of  7 variables:
  ..$ Sector        : chr [1:134] "Customer Segements" NA NA NA ...
  ..$ Subject       : chr [1:134] "Waste Generators" NA NA NA ...
  ..$ Variable      : chr [1:134] "Residential (Household)" "Residential (Apartment)" "Commercial (Dukas)" "Commercial (Bandas)" ...
  ..$ Yes.No.NA     : chr [1:134] "12" "22" "0" "0" ...
  ..$ Revenue.Strems: num [1:134] 2400 39600 NA NA NA NA 150 NA NA NA ...
  ..$ Cost.Stucture : num [1:134] NA NA NA NA NA NA NA NA NA NA ...
  ..$ Notes         : chr [1:134] NA "In almost all their apartments they are devided into 6 section/wings where each wing pays KES 300 per month" NA NA ...
 $ Driver Conductor     :'data.frame':  134 obs. of  7 variables:
  ..$ Sector         : chr [1:134] "Customer Segements" NA NA NA ...
  ..$ Subject        : chr [1:134] "Waste Generators" NA NA NA ...
  ..$ Variable       : chr [1:134] "Residential (Household)" "Residential (Apartment)" "Commercial (Dukas)" "Commercial (Bandas)" ...
  ..$ Yes.No.NA      : chr [1:134] "138" "1" "4" "5" ...
  ..$ Revenue.Streams: num [1:134] 5520 200 200 250 100 NA NA 400 NA NA ...
  ..$ Cost.Structure : num [1:134] NA NA NA NA NA NA NA NA NA NA ...
  ..$ Notes          : chr [1:134] NA NA NA NA ...
 $ District Scouts      :'data.frame':  134 obs. of  7 variables:
  ..$ Sector         : chr [1:134] "Customer Segements" NA NA NA ...
  ..$ Subject        : chr [1:134] "Waste Generators" NA NA NA ...
  ..$ Variable       : chr [1:134] "Residential (Household)" "Residential (Apartment)" "Commercial (Dukas)" "Commercial (Bandas)" ...
  ..$ Yes.No.NA      : chr [1:134] "150" "No" "15" "20" ...
  ..$ Revenue.Streams: num [1:134] 79950 NA 2400 4800 NA ...
  ..$ Cost.Structure : num [1:134] NA NA NA NA NA NA NA NA NA NA ...
  ..$ Notes          : chr [1:134] NA NA NA NA ...
 $ Ganjoni Youth        :'data.frame':  134 obs. of  7 variables:
  ..$ Sector         : chr [1:134] "Customer Segements" NA NA NA ...
  ..$ Subject        : chr [1:134] "Waste Generators" NA NA NA ...
  ..$ Variable       : chr [1:134] "Residential (Household)" "Residential (Apartment)" "Commercial (Dukas)" "Commercial (Bandas)" ...
  ..$ Yes.No.NA      : chr [1:134] "150" "No" "8" "11" ...
  ..$ Revenue.Streams: num [1:134] 4500 NA 240 440 NA NA NA 300 100 NA ...
  ..$ Cost.Structure : num [1:134] NA NA NA NA NA NA NA NA NA NA ...
  ..$ Notes          : chr [1:134] NA NA NA NA ...
 $ Grandi Youth Bunge   :'data.frame':  134 obs. of  7 variables:
  ..$ Sector         : chr [1:134] "Customer Segements" NA NA NA ...
  ..$ Subject        : chr [1:134] "Waste Generators" NA NA NA ...
  ..$ Variable       : chr [1:134] "Residential (Household)" "Residential (Apartment)" "Commercial (Dukas)" "Commercial (Bandas)" ...
  ..$ Yes.No.NA      : chr [1:134] "630" "No" "50" "10" ...
  ..$ Revenue.Stream.: num [1:134] 151200 NA 12000 2400 NA ...
  ..$ Cost.Structure : num [1:134] NA NA NA NA NA NA NA NA NA NA ...
  ..$ Notes          : chr [1:134] NA NA NA NA ...
 $ King Orani Youth     :'data.frame':  134 obs. of  7 variables:
  ..$ Sector         : chr [1:134] "Customer Segements" NA NA NA ...
  ..$ Subject        : chr [1:134] "Waste Generators" NA NA NA ...
  ..$ Variable       : chr [1:134] "Residential (Household)" "Residential (Apartment)" "Commercial (Dukas)" "Commercial (Bandas)" ...
  ..$ Yes.No.NA      : chr [1:134] "200" "20" "4" "2" ...
  ..$ Revenue.Streams: num [1:134] 40000 6000 1600 800 NA NA NA NA 400 NA ...
  ..$ Cost.Stucture  : num [1:134] NA NA NA NA NA NA NA NA NA NA ...
  ..$ Notes          : chr [1:134] NA NA NA NA ...
 $ Magongo Santana      :'data.frame':  134 obs. of  7 variables:
  ..$ Sector         : chr [1:134] "Customer Segements" NA NA NA ...
  ..$ Subject        : chr [1:134] "Waste Generators" NA NA NA ...
  ..$ Variable       : chr [1:134] "Residential (Household)" "Residential (Apartment)" "Commercial (Dukas)" "Commercial (Bandas)" ...
  ..$ Yes.No.NA      : chr [1:134] "10" "8" "6" "N/A" ...
  ..$ Revenue.Streams: num [1:134] 8800 1280 1200 NA NA NA NA NA NA NA ...
  ..$ Cost.Stucture  : num [1:134] NA NA NA NA NA NA NA NA NA NA ...
  ..$ Notes          : chr [1:134] NA NA NA NA ...
 $ Mbuyuni Youth        :'data.frame':  134 obs. of  7 variables:
  ..$ Sector         : chr [1:134] "Customer Segements" NA NA NA ...
  ..$ Subject        : chr [1:134] "Waste Generators" NA NA NA ...
  ..$ Variable       : chr [1:134] "Residential (Household)" "Residential (Apartment)" "Commercial (Dukas)" "Commercial (Bandas)" ...
  ..$ Yes.No.NA      : chr [1:134] "150" "0" "50" "0" ...
  ..$ Revenue.Streams: num [1:134] 15000 NA 3000 NA NA 800 NA NA NA NA ...
  ..$ Cost.Stucture  : num [1:134] NA NA NA NA NA NA NA NA NA NA ...
  ..$ Notes          : chr [1:134] NA NA NA NA ...
 $ More Flow Enterprises:'data.frame':  134 obs. of  7 variables:
  ..$ Sector         : chr [1:134] "Customer Segements" NA NA NA ...
  ..$ Subject        : chr [1:134] "Waste Generators" NA NA NA ...
  ..$ Variable       : chr [1:134] "Residential (Household)" "Residential (Apartment)" "Commercial (Dukas)" "Commercial (Bandas)" ...
  ..$ Yes.No.NA      : chr [1:134] "No" "409" "6" "No" ...
  ..$ Revenue.Streams: chr [1:134] NA "349,000" NA NA ...
  ..$ Cost.Structure : num [1:134] NA NA NA NA NA NA NA NA NA NA ...
  ..$ Notes          : chr [1:134] "Company signs contracts only with landlords/resident agents. Data concerning the value of these contracts is not currently avai"| __truncated__ NA NA NA ...
 $ Mukono Self Help     :'data.frame':  134 obs. of  7 variables:
  ..$ Sector         : chr [1:134] "Customer Segements" NA NA NA ...
  ..$ Subject        : chr [1:134] "Waste Generators" NA NA NA ...
  ..$ Variable       : chr [1:134] "Residential (Household)" "Residential (Apartment)" "Commercial (Dukas)" "Commercial (Bandas)" ...
  ..$ Yes.No.NA      : chr [1:134] "40" "No" "6" "4" ...
  ..$ Revenue.Streams: num [1:134] 6000 NA 1200 600 NA NA NA 300 400 NA ...
  ..$ Cost.Structure : num [1:134] NA NA NA NA NA NA NA NA NA NA ...
  ..$ Notes          : chr [1:134] NA NA NA NA ...
 $ Mombasa Youth Network:'data.frame':  134 obs. of  7 variables:
  ..$ Sector        : chr [1:134] "Customer Segements" NA NA NA ...
  ..$ Subject       : chr [1:134] "Waste Generators" NA NA NA ...
  ..$ Variable      : chr [1:134] "Residential (Household)" "Residential (Apartment)" "Commercial (Dukas)" "Commercial (Bandas)" ...
  ..$ Yes.No.NA     : chr [1:134] "No" "76" "No" "No" ...
  ..$ Revenue.Steams: num [1:134] NA 3800 NA NA NA NA NA NA NA NA ...
  ..$ Cost.Structure: num [1:134] NA NA NA NA NA NA NA NA NA NA ...
  ..$ Notes         : chr [1:134] NA NA NA NA ...
 $ OneWorld Youth       :'data.frame':  134 obs. of  7 variables:
  ..$ Sector         : chr [1:134] "Customer Segements" NA NA NA ...
  ..$ Subject        : chr [1:134] "Waste Generators" NA NA NA ...
  ..$ Variable       : chr [1:134] "Residential (Household)" "Residential (Apartment)" "Commercial (Dukas)" "Commercial (Bandas)" ...
  ..$ Yes.No.NA      : chr [1:134] "50" "No" "15" "10" ...
  ..$ Revenue.Streams: num [1:134] 1000 NA 300 500 NA NA NA 20 NA NA ...
  ..$ Cost.Structures: num [1:134] NA NA NA NA NA NA NA NA NA NA ...
  ..$ Notes          : chr [1:134] NA NA NA NA ...
 $ Quatet               :'data.frame':  134 obs. of  7 variables:
  ..$ Sector         : chr [1:134] "Customer Segements" NA NA NA ...
  ..$ Subject        : chr [1:134] "Waste Generators" NA NA NA ...
  ..$ Variable       : chr [1:134] "Residential (Household)" "Residential (Apartment)" "Commercial (Dukas)" "Commercial (Bandas)" ...
  ..$ Yes.No.NA      : chr [1:134] "12" "22" "0" "0" ...
  ..$ Revenue.Streams: num [1:134] 2400 39600 NA NA NA NA 150 NA NA NA ...
  ..$ Cost.Stucture  : num [1:134] NA NA NA NA NA NA NA NA NA NA ...
  ..$ Notes          : chr [1:134] NA NA NA NA ...
 $ Safi Youth Group     :'data.frame':  134 obs. of  7 variables:
  ..$ Sector         : chr [1:134] "Customer Segements" NA NA NA ...
  ..$ Subject        : chr [1:134] "Waste Generators" NA NA NA ...
  ..$ Variable       : chr [1:134] "Residential (Household)" "Residential (Apartment)" "Commercial (Dukas)" "Commercial (Bandas)" ...
  ..$ Yes.No.NA      : chr [1:134] "250" "700" "0" "0" ...
  ..$ Revenue.Streams: num [1:134] 25000 140000 NA NA NA NA NA 3500 NA NA ...
  ..$ Cost.Stucture  : chr [1:134] NA NA NA NA ...
  ..$ Notes          : chr [1:134] NA NA NA NA ...
 $ Sent Kumi Youth      :'data.frame':  134 obs. of  7 variables:
  ..$ Sector         : chr [1:134] "Customer Segements" NA NA NA ...
  ..$ Subject        : chr [1:134] "Waste Generators" NA NA NA ...
  ..$ Variable       : chr [1:134] "Residential (Household)" "Residential (Apartment)" "Commercial (Dukas)" "Commercial (Bandas)" ...
  ..$ Yes.No.NA      : chr [1:134] "186" "4" "8" "4" ...
  ..$ Revenue.Streams: num [1:134] 18600 400 1280 480 NA NA NA 160 200 NA ...
  ..$ Cost.structure : num [1:134] NA NA NA NA NA NA NA NA NA NA ...
  ..$ Notes          : chr [1:134] NA NA NA NA ...
 $ Smart Guys           :'data.frame':  134 obs. of  7 variables:
  ..$ Sector         : chr [1:134] "Customer Segements" NA NA NA ...
  ..$ Subject        : chr [1:134] "Waste Generators" NA NA NA ...
  ..$ Variable       : chr [1:134] "Residential (Household)" "Residential (Apartment)" "Commercial (Dukas)" "Commercial (Bandas)" ...
  ..$ Yes.No.NA      : chr [1:134] "100" "No" "No" "2" ...
  ..$ Revenue.Streams: num [1:134] 12000 NA NA 160 NA NA NA NA NA NA ...
  ..$ Cost.Structure : num [1:134] NA NA NA NA NA NA NA NA NA NA ...
  ..$ Notes          : chr [1:134] NA NA NA NA ...
 $ Soweto Self Help     :'data.frame':  134 obs. of  7 variables:
  ..$ Sector         : chr [1:134] "Customer Segements" NA NA NA ...
  ..$ Subject        : chr [1:134] "Waste Generators" NA NA NA ...
  ..$ Variable       : chr [1:134] "Residential (Household)" "Residential (Apartment)" "Commercial (Dukas)" "Commercial (Bandas)" ...
  ..$ Yes.No.NA      : chr [1:134] "100" "N/A" "1" "8" ...
  ..$ Revenue.Streams: num [1:134] 14000 NA 60 640 NA NA NA NA NA NA ...
  ..$ Cost.Stucture  : num [1:134] NA NA NA NA NA NA NA NA NA NA ...
  ..$ Notes          : chr [1:134] NA NA NA NA ...
 $ Stretchers           :'data.frame':  134 obs. of  7 variables:
  ..$ Sector         : chr [1:134] "Customer Segements" NA NA NA ...
  ..$ Subject        : chr [1:134] "Waste Generators" NA NA NA ...
  ..$ Variable       : chr [1:134] "Residential (Household)" "Residential (Apartment)" "Commercial (Dukas)" "Commercial (Bandas)" ...
  ..$ Yes.No.NA      : chr [1:134] "22" "No" "8" "4" ...
  ..$ Revenue.Streams: num [1:134] 2200 NA 800 200 NA NA NA 200 NA NA ...
  ..$ Cost.Structure.: num [1:134] NA NA NA NA NA NA NA NA NA NA ...
  ..$ Notes          : chr [1:134] NA NA NA NA ...
 $ Taka ni Mali         :'data.frame':  134 obs. of  7 variables:
  ..$ Sector         : chr [1:134] "Customer Segements" NA NA NA ...
  ..$ Subject        : chr [1:134] "Waste Generators" NA NA NA ...
  ..$ Variable       : chr [1:134] "Residential (Household)" "Residential (Apartment)" "Commercial (Dukas)" "Commercial (Bandas)" ...
  ..$ Yes.No.NA      : chr [1:134] "396" "4" "8" "6" ...
  ..$ Revenue.Streams: num [1:134] 59400 4000 1600 2400 0 0 600 300 900 0 ...
  ..$ Cost.Stucture  : num [1:134] NA NA NA NA NA NA NA NA NA NA ...
  ..$ Notes          : chr [1:134] NA NA NA NA ...
 $ Tuliza               :'data.frame':  134 obs. of  7 variables:
  ..$ Sector         : chr [1:134] "Customer Segements" NA NA NA ...
  ..$ Subject        : chr [1:134] "Waste Generators" NA NA NA ...
  ..$ Variable       : chr [1:134] "Residential (Household)" "Residential (Apartment)" "Commercial (Dukas)" "Commercial (Bandas)" ...
  ..$ Yes.No.NA      : chr [1:134] "90" "3" "N/A" "2" ...
  ..$ Revenue.Streams: num [1:134] 16200 4500 NA 200 NA NA NA 400 400 NA ...
  ..$ Cost.Stucture  : num [1:134] NA NA NA NA NA NA NA NA NA NA ...
  ..$ Notes          : chr [1:134] "The 18 households are single bedroom houses and they pay KES 100 per  month while the rest are double bedroom that pay KES 200 "| __truncated__ NA NA NA ...
 $ Zama Uzuke           :'data.frame':  134 obs. of  7 variables:
  ..$ Sector         : chr [1:134] "Customer Segements" NA NA NA ...
  ..$ Subject        : chr [1:134] "Waste Generators" NA NA NA ...
  ..$ Variable       : chr [1:134] "Residential (Household)" "Residential (Apartment)" "Commercial (Dukas)" "Commercial (Bandas)" ...
  ..$ Yes.No.NA      : chr [1:134] "17" "12" "10" "No" ...
  ..$ Revenue.Streams: num [1:134] 4080 2400 2400 NA NA NA NA 1600 1600 NA ...
  ..$ Cost.Structure : num [1:134] NA NA NA NA NA NA NA NA NA NA ...
  ..$ Notes          : chr [1:134] NA NA NA NA ...

我希望我的最终数据框看起来像这样

部门|主题|变量|是的.No.NA |收入...... |成本...... |备注|名称*

*其中name是一个新变量,表示原始数据帧的名称。

N.B。 “变量”是这里的关键指标(有134个不同的变量)。

真的希望这是有道理的,并且如果在其他地方已经回答道歉 - 我确实尝试在SE的其他地方找到一些答案。

由于

玛蒂

2 个答案:

答案 0 :(得分:2)

您可以先使用

添加名称
sheet_list <- mapply(function(df, name) {
                           df$Name <- rep(name, nrow(df))
                           return(df)
                       }, df = sheet_list, name = names(sheet_list),
                          SIMPLIFY = FALSE)

然后,将所有data.frames放到一个

all_df <- do.call("rbind", sheet_list)

另一个选项, 您还可以在调用do.call后添加姓名:

all_df <- do.call("rbind", sheet_list)
all_df$Name <- gsub("\\.\\d+$", "", row.names(all_df))

修改
如果所有data.frames&#39; colnames不一样,你可以做,作为第一步,(例如,为所有data.frames提供与第一个data.frame相同的colnames):

sheet_list<-lapply(sheet_list, function(x) {colnames(x) <- colnames(sheet_list[[1]]) ; return(x)})

答案 1 :(得分:1)

这是另一种方式。如果您在环境(例如,全局环境)中拥有所有数据框,则可以使用mget()获取所有数据框并创建列表。然后,您可以在unnest()包中使用tidyr;您可以使用此功能创建一个包含数据框名称的列。我创建了一个简单的示例数据并执行了以下操作。希望这会对你有所帮助。

Alliance <- data.frame(Sector = "Customer Segements",
                       Subject = "Waste Generators",
                       stringsAsFactors = FALSE)


Bidii <- data.frame(Sector = "Customer Segements",
                    Subject = "Waste Generators",
                    stringsAsFactors = FALSE)

# I do not know what kind of patterns you have. You may need to adjust this part.
mylist <- mget(ls(pattern = "^.*"))

# mylist
#$Alliance
#              Sector          Subject
#1 Customer Segements Waste Generators
#
#$Bidii
#              Sector          Subject
#1 Customer Segements Waste Generators

library(tidyr)
unnest(mylist, names)

#     names             Sector          Subject
#1 Alliance Customer Segements Waste Generators
#2    Bidii Customer Segements Waste Generators