Search code examples
rreadxl

adding a column across a list of tibbles


I am reading using readxl to bring data into R as a list of tibbles.

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

xlsx_example<-readxl_example("datasets.xlsx")

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

This then brings up 4 tibbles, [[1]] through [[4]]].

I would like to add a new column to all four, with a unique label in each. if it were a single data frame I would use

AllSheets%Newcolumn<-"number1"

But this does not work when you have a list of tibbles. Is there a way to add NewColumn to all the sheets, with "number1", "number2", etc in each sheet?


Solution

  • You can use Map for this:

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

    And the output

    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" ...