0

I have a list, list_df, and I'd like to add a column using mutate to each element within the list to reflect which element it is located in.

df1 <- tibble(M = words[sample(1:length(words), 10)],
                  N = rnorm(10, 3, 3)) 

df2 <- tibble(M = words[sample(1:length(words), 10)],
              N = rnorm(10, 3, 3))

df3 <- tibble(M = words[sample(1:length(words), 10)],
              N = rnorm(10, 3, 3))

list_df <- list(df1, df2, df3)

I can't seem to find a function that can extract the element names, similar to rownames(). I think the code would have to involve map( ~ mutate(.))?

The third element in my end output would look like this:

[[3]]
# A tibble: 10 x 3
   M              N  page
   <chr>      <dbl> <dbl>
 1 really     9.86      3
 2 son        3.34      3
 3 fair       0.660     3
 4 local      0.664     3
 5 out        3.76      3
 6 high       6.62      3
 7 condition  5.24      3
 8 cat        1.03      3
 9 meaning    3.39      3
10 account   -0.702     3
Desmond
  • 1,047
  • 7
  • 14

2 Answers2

2

You can use imap :

library(dplyr)
library(purrr)
imap(list_df, ~.x %>% mutate(page = .y))

Or Map in base R :

Map(cbind, list_df, page = seq_along(list_df))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
0

Perhaps an onther option... create the list with tibble::lst(), this automatically adds the objectname to the names of the list

tibble::lst(df1,df2,df3)

results in:

$df1
# A tibble: 10 x 2
   M             N
   <chr>     <dbl>
 1 shoot      1.76
 2 house      3.50
 3 compare    3.61
 4 garden     5.40
 5 colleague  2.33
 6 park       1.65
 7 rather     2.50
 8 sick       4.74
 9 role       9.83
10 electric  -1.42

$df2
# A tibble: 10 x 2
   M               N
   <chr>       <dbl>
 1 unless      0.683
 2 due        -2.72 
 3 particular  5.26 
 4 minister    3.05 
 5 germany     2.21 
 6 employ      0.185
 7 wood       -0.537
 8 street      3.43 
 9 without    -0.629
10 tie         4.66 

$df3
# A tibble: 10 x 2
   M               N
   <chr>       <dbl>
 1 lunch      -0.624
 2 million     2.49 
 3 double      5.74 
 4 appoint     3.18 
 5 television -3.36 
 6 saturday    1.60 
 7 respect     5.12 
 8 allow       4.55 
 9 comment     1.47 
10 suit        4.49 

If you later want to rowbind the list together, you can usee a function like data.table::rbindlist() to extract the list-element's names to a column with the idcol-argument.

data.table::rbindlist( tibble::lst(df1,df2,df3), idcol = "df_name" )

results in

#      df_name          M          N
#  1:      df1      shoot  1.7580908
#  2:      df1      house  3.4973056
#  3:      df1    compare  3.6061300
#  4:      df1     garden  5.3983339
#  5:      df1  colleague  2.3278449
#  6:      df1       park  1.6549181
#  7:      df1     rather  2.4975684
#  8:      df1       sick  4.7351671
#  9:      df1       role  9.8348196
# 10:      df1   electric -1.4212905
# 11:      df2     unless  0.6831733
# 12:      df2        due -2.7249668
# 13:      df2 particular  5.2616224
# 14:      df2   minister  3.0530140
# 15:      df2    germany  2.2128920
# 16:      df2     employ  0.1850958
# 17:      df2       wood -0.5370465
# 18:      df2     street  3.4334869
# 19:      df2    without -0.6292031
# 20:      df2        tie  4.6573207
# 21:      df3      lunch -0.6241718
# 22:      df3    million  2.4854351
# 23:      df3     double  5.7427438
# 24:      df3    appoint  3.1819854
# 25:      df3 television -3.3639328
# 26:      df3   saturday  1.6013188
# 27:      df3    respect  5.1175062
# 28:      df3      allow  4.5501064
# 29:      df3    comment  1.4678056
# 30:      df3       suit  4.4915864
#      df_name          M          N
Wimpel
  • 26,031
  • 1
  • 20
  • 37