[英]Manipulate multiple data frames in R
I have over 40 data frame in R with the same exact variables. 我在R中有40多个具有相同确切变量的数据框。 I have manipulated one of them and I would like to do those manipulations to all of them.
我已经操纵了其中一个,并且我想对所有人进行操纵。 So far I've created a list of the data frames
到目前为止,我已经创建了数据帧列表
dataframes <- ls(pattern = "file_")
(All of the data frames started with file_) (所有数据帧均以file_开头)
But when I run 但是当我跑步时
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)
})
It returns 它返回
Error in x$Distance : $ operator is invalid for atomic vectors
Here is a look at one of the data frames. 这是其中一个数据帧的外观。 This is the data contained in one of the data frames.
这是包含在数据帧之一中的数据。
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]
To access a data frame by its name use get()
as in get("file_x")
. 要按其名称访问数据帧,请像
get("file_x")
一样使用get()
get("file_x")
。 With multiple data frames use mget()
. 对于多个数据帧,请使用
mget()
。 In your case I think you want: 就您而言,我认为您想要:
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)
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.