【问题标题】:adding a column across a list of tibbles在小标题列表中添加一列
【发布时间】:2018-06-20 13:27:25
【问题描述】:

我正在阅读使用 readxl 将数据作为小标题列表带入 R。

library("readxl") 
library("reshape2")

xlsx_example<-readxl_example("datasets.xlsx")

AllSheets<-lapply(excel_sheets(xlsx_example), read_excel,    path=xlsx_example)
AllSheets

这会调出 4 个小标题,从 [[1]] 到 [[4]]]。

我想为所有四个添加一个新列,每个列都有一个唯一的标签。如果是单个数据框,我会使用

AllSheets%Newcolumn<-"number1"

但是当你有一个小标题列表时,这不起作用。有没有办法将 NewColumn 添加到所有工作表中,每个工作表中都有“number1”、“number2”等?

【问题讨论】:

    标签: r readxl


    【解决方案1】:

    您可以为此使用Map

    newdf <- Map(function(x, y) {
      x$Newcolumn <- y
      x
    },
      AllSheets, 
      c('Number1', 'Number2', 'Number3', 'Number4'))
    

    还有输出

    List of 4
     $ :Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   150 obs. of  6 variables:
      ..$ Sepal.Length: num [1:150] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
      ..$ Sepal.Width : num [1:150] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
      ..$ Petal.Length: num [1:150] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
      ..$ Petal.Width : num [1:150] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
      ..$ Species     : chr [1:150] "setosa" "setosa" "setosa" "setosa" ...
      ..$ Newcolumn   : chr [1:150] "Number1" "Number1" "Number1" "Number1" ...
     $ :Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   32 obs. of  12 variables:
      ..$ mpg      : num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
      ..$ cyl      : num [1:32] 6 6 4 6 8 6 8 4 4 6 ...
      ..$ disp     : num [1:32] 160 160 108 258 360 ...
      ..$ hp       : num [1:32] 110 110 93 110 175 105 245 62 95 123 ...
      ..$ drat     : num [1:32] 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
      ..$ wt       : num [1:32] 2.62 2.88 2.32 3.21 3.44 ...
      ..$ qsec     : num [1:32] 16.5 17 18.6 19.4 17 ...
      ..$ vs       : num [1:32] 0 0 1 1 0 1 0 1 1 1 ...
      ..$ am       : num [1:32] 1 1 1 0 0 0 0 0 0 0 ...
      ..$ gear     : num [1:32] 4 4 4 3 3 3 3 4 4 4 ...
      ..$ carb     : num [1:32] 4 4 1 1 2 1 4 2 2 4 ...
      ..$ Newcolumn: chr [1:32] "Number2" "Number2" "Number2" "Number2" ...
     $ :Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   71 obs. of  3 variables:
      ..$ weight   : num [1:71] 179 160 136 227 217 168 108 124 143 140 ...
      ..$ feed     : chr [1:71] "horsebean" "horsebean" "horsebean" "horsebean" ...
      ..$ Newcolumn: chr [1:71] "Number3" "Number3" "Number3" "Number3" ...
     $ :Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   1000 obs. of  6 variables:
      ..$ lat      : num [1:1000] -20.4 -20.6 -26 -18 -20.4 ...
      ..$ long     : num [1:1000] 182 181 184 182 182 ...
      ..$ depth    : num [1:1000] 562 650 42 626 649 195 82 194 211 622 ...
      ..$ mag      : num [1:1000] 4.8 4.2 5.4 4.1 4 4 4.8 4.4 4.7 4.3 ...
      ..$ stations : num [1:1000] 41 15 43 19 11 12 43 15 35 19 ...
      ..$ Newcolumn: chr [1:1000] "Number4" "Number4" "Number4" "Number4" ...
    

    【讨论】:

      猜你喜欢
      • 2023-01-28
      • 1970-01-01
      • 1970-01-01
      • 2020-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多