在R

时间:2015-07-27 21:28:20

标签: r dataframe

我在R中有超过40个具有相同确切变量的数据帧。我操纵了其中一个,我想对所有人进行操作。 到目前为止,我已经创建了一个数据框列表

dataframes <- ls(pattern = "file_")

(所有数据框都以file_开头)

但是当我跑步时

lapply(dataframes,function(x){
x$bin <- cut(x$Distance, breaks  = c(0, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 750, 1000, 1250, 
                                              1500, 2000, 2500, 6000), labels = NULL, include.lowest = T)
return(x)  
})

返回

Error in x$Distance : $ operator is invalid for atomic vectors

以下是其中一个数据框。 这是其中一个数据框中包含的数据。

Coast   STCG.2.Commodity    Port.of.Entry       Domestic.Destinations Distance
    1 East/Gulf Alcoholicbeverages Baltimore MD MSA               Albany NY CSA    285.8
    2 East/Gulf Alcoholicbeverages Baltimore MD MSA Atlanta GA-AL CSA (GA Part)    586.0
    3 East/Gulf Alcoholicbeverages Baltimore MD MSA               Austin TX MSA   1344.5
    4 East/Gulf Alcoholicbeverages Baltimore MD MSA               Austin TX MSA   1344.5
    5 East/Gulf Alcoholicbeverages Baltimore MD MSA               Austin TX MSA   1344.5
    6 East/Gulf Alcoholicbeverages Baltimore MD MSA            Baltimore MD MSA      0.0
                                                                        Index         Domestic.Mode Total.Ktons   Ton_Share Total.Ton.Mile Total.M.
    1               East/GulfAlcoholic beveragesBaltimore MD MSAAlbany NY CSA                 Truck      0.0005           1     0.00019568   0.0022
    2 East/GulfAlcoholic beveragesBaltimore MD MSAAtlanta GA-AL CSA (GA Part)                 Truck      0.2639           1     0.17538398   0.1922
    3               East/GulfAlcoholic beveragesBaltimore MD MSAAustin TX MSA                 Truck      1.0548 0.419804187     1.64523022   0.5473
    4               East/GulfAlcoholic beveragesBaltimore MD MSAAustin TX MSA                  Rail      0.9675 0.385059301     1.69025058   0.5020
    5               East/GulfAlcoholic beveragesBaltimore MD MSAAustin TX MSA Multiple modes & mail      0.4903 0.195136512     0.88638634   0.2544
    6            East/GulfAlcoholic beveragesBaltimore MD MSABaltimore MD MSA                 Truck     52.7997 0.999873121     1.24978071 205.7857
      Total.Current.M.                bin
    1           0.0025          (250,300]
    2           0.2128          (500,750]
    3           0.6061 (1.25e+03,1.5e+03]
    4           0.5560 (1.25e+03,1.5e+03]
    5           0.2818 (1.25e+03,1.5e+03]
    6         227.9075             [0,50]

1 个答案:

答案 0 :(得分:1)

要按名称访问数据框,请使用get()中的get("file_x")。使用多个数据框使用mget()。在你的情况下,我认为你想要:

dataframes <- mget(ls(pattern = "file_"))

lapply(dataframes,function(x){
x$bin <- cut(x$Distance, breaks  = c(0, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 750, 1000, 1250, 
                                          1500, 2000, 2500, 6000), labels = NULL, include.lowest = T)
return(x)  
})