Reputation: 309
My list has the following dataframes. I did not name them when I created the list
str(train_data)
List of 16
$ :'data.frame': 72 obs. of 2 variables:
..$ DateTime: Date[1:72], format: "2009-07-31" "2009-08-31" "2009-09-30" ...
..$ Value : num [1:72] 49.9 53.5 54.4 56 54.4 55.3 57.2 55.8 58.8 58.1 ...
$ :'data.frame': 72 obs. of 2 variables:
..$ DateTime: Date[1:72], format: "2009-07-31" "2009-08-31" "2009-09-30" ...
..$ Value : num [1:72] 4555 5014 4647 5121 6461 ...
$ :'data.frame': 72 obs. of 2 variables:
..$ DateTime: Date[1:72], format: "2009-07-31" "2009-08-31" "2009-09-30" ...
..$ Value : num [1:72] 215 215 216 217 217 ...
$ :'data.frame': 72 obs. of 2 variables:
..$ DateTime: Date[1:72], format: "2009-07-31" "2009-08-31" "2009-09-30" ...
..$ Value : num [1:72] 5399 5374 5560 5517 5386 ...
$ :'data.frame': 72 obs. of 2 variables:
..$ DateTime: Date[1:72], format: "2009-07-31" "2009-08-31" "2009-09-30" ...
..$ Value : num [1:72] 139901 139492 138818 138432 138659 ...
$ :'data.frame': 72 obs. of 2 variables:
..$ DateTime: Date[1:72], format: "2009-07-31" "2009-08-31" "2009-09-30" ...
..$ Value : num [1:72] 117 118 118 118 119 ...
$ :'data.frame': 72 obs. of 2 variables:
..$ DateTime: Date[1:72], format: "2009-07-31" "2009-08-31" "2009-09-30" ...
..$ Value : num [1:72] 0.9 0.4 -0.2 -0.6 -0.7 -0.5 -0.4 -0.2 0.2 0.5 ...
$ :'data.frame': 72 obs. of 2 variables:
..$ DateTime: Date[1:72], format: "2009-07-31" "2009-08-31" "2009-09-30" ...
..$ Value : num [1:72] 119 121 121 122 124 ...
$ :'data.frame': 72 obs. of 2 variables:
..$ DateTime: Date[1:72], format: "2009-07-01" "2009-08-03" "2009-09-01" ...
..$ Value : num [1:72] 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25 ...
$ :'data.frame': 72 obs. of 2 variables:
..$ DateTime: Date[1:72], format: "2009-07-31" "2009-08-31" "2009-09-30" ...
..$ Value : num [1:72] 411 418 386 396 375 352 345 336 381 422 ...
$ :'data.frame': 68 obs. of 2 variables:
..$ DateTime: Date[1:68], format: "2009-11-30" "2009-12-31" "2010-01-31" ...
..$ Value : num [1:68] 100 100 101 101 101 ...
$ :'data.frame': 72 obs. of 2 variables:
..$ DateTime: Date[1:72], format: "2009-07-31" "2009-08-31" "2009-09-30" ...
..$ Value : num [1:72] 0.2 1.9 -2.4 1 1 0.4 0 0.2 2.2 0.7 ...
$ :'data.frame': 72 obs. of 2 variables:
..$ DateTime: Date[1:72], format: "2009-07-31" "2009-08-31" "2009-09-30" ...
..$ Value : num [1:72] 14601 14814 15009 15352 15219 ...
$ :'data.frame': 72 obs. of 2 variables:
..$ DateTime: Date[1:72], format: "2009-07-31" "2009-08-31" "2009-09-30" ...
..$ Value : num [1:72] 11.39 14.58 9.38 10.41 10.86 ...
$ :'data.frame': 72 obs. of 2 variables:
..$ DateTime: Date[1:72], format: "2009-07-31" "2009-08-31" "2009-09-30" ...
..$ Value : num [1:72] -4.43 -4.72 -4.83 -4.81 -3.74 -2.16 -0.6 0.03 1.02 1.36 ...
$ :'data.frame': 72 obs. of 2 variables:
..$ DateTime: Date[1:72], format: "2009-07-31" "2009-08-31" "2009-09-30" ...
..$ Value : num [1:72] 0 0.3 0.2 0.3 0.3 0.1 0.1 -0.1 0 0 ...
Now I want to create a data frame which will have the first column containing the names of these data frames but I cant seem to find their names. Should I be naming all the 16 data frames manually (This would not be efficient)
Thank You.
Upvotes: 1
Views: 63
Reputation: 887008
If we need to create a column of 'names' for each data.frame
in the list
(based on the str
in OP's post, there are no names
for the data.frame
, so we can name it by paste
ing the 'dat' string with the sequence of train_data
), we can use Map
to create a column of 'Names' in each of the data.frame
Map(cbind, Names = paste0('dat', seq_along(train_data)), train_data)
If we have used the list.files
to get the names of the files, we strip off the .csv
part with sub
and name the 'train_data' list
with that, and use Map
nm1 <- sub("\\.csv", "", temp)
names(train_data) <- nm1
Map(cbind, Names = names(train_data), train_data)
Or just
Map(cbind, Names = nm1, train_data)
Upvotes: 1