如何获取列表中数据框的名称

时间:2016-08-04 10:06:31

标签: r 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 ...

现在我想创建一个数据框,其中第一列包含这些数据框的名称,但我似乎无法找到它们的名称。我应该手动命名所有16个数据帧(这不会有效)

谢谢。

1 个答案:

答案 0 :(得分:1)

如果我们需要创建一个'名称列?对于data.frame中的每个list(根据OP帖子中的strnames没有data.frame,因此我们可以使用paste的序列train_data来命名,我们可以使用Map创建一个'名称&#39列;在每个data.frame

Map(cbind, Names = paste0('dat', seq_along(train_data)), train_data)

更新

如果我们使用list.files获取文件名称,我们会使用.csv删除sub部分,并将' train_data命名为' list,然后使用Map

nm1 <- sub("\\.csv", "", temp)
names(train_data) <- nm1
Map(cbind, Names = names(train_data), train_data)

或者只是

Map(cbind, Names = nm1, train_data)