R,使用load()从.rda对象分配内容

时间:2016-01-21 13:52:20

标签: r types load rda

这是非常基本的(我怀疑这已在其他地方被问到,虽然不是here)。

我有大量的.rda文件,每个文件都有一个数据框。我想对每个数据框进行计算,因此需要加载它们(load())。如果他们是.RDS对象我会喜欢:

#My data
x <- data.frame(a=1:3)
y <- data.frame(a=3:6)

#Save as RDS 
saveRDS(x, file = "x.rds")
saveRDS(y, file = "y.rds")

files <- c("x.rds", "y.rds")
data <- lapply(files, readRDS)

#Do something with the data in the list "data"

如何使用load执行类似的操作,因为您无法将数据(仅名称)分配给变量:

x <- data.frame(a=1:3)

> x
  a
1 1
2 2
3 3

save(x, file= "x.rda")
x <- load("x.rda")

> x
[1] "x"

2 个答案:

答案 0 :(得分:3)

如果您确定所有文件只包含一个对象,则可以在这样的包装函数中利用envir load参数:

load_object <- function(file) {
  tmp <- new.env()
  load(file = file, envir = tmp)
  tmp[[ls(tmp)[1]]]
}

用法如下:

not_x <- data.frame(xx = 1:5)
save(not_x, file = "~/tmp/x.Rdata") 

(x <- load_object("~/tmp/x.Rdata"))
#  xx
#1  1
#2  2
#3  3
#4  4
#5  5

all.equal(not_x, x)
#[1] TRUE

答案 1 :(得分:0)

您希望使用基本dfs函数,例如:

df = pd.read_html('https://en.wikipedia.org/wiki/List_of_shopping_malls_in_the_South_Florida_metropolitan_area',
                   header=0 )[0]
print df

                                  Name                           Location
0                        Aventura Mall                           Aventura
1                    Bal Harbour Shops                        Bal Harbour
2                  Bayside Marketplace                     Downtown Miami
3                   Boynton Beach Mall                      Boynton Beach
4                            CityPlace                    West Palm Beach
5                             CocoWalk               Coconut Grove, Miami
6                         Coral Square                      Coral Springs
7                        Dadeland Mall                            Kendall
8                         Dolphin Mall                         Sweetwater
9              Downtown at the Gardens                 Palm Beach Gardens
10                           The Falls                            Kendall
11          Galeria International Mall                     Downtown Miami
12     The Galleria at Fort Lauderdale                    Fort Lauderdale
13                    The Gardens Mall                 Palm Beach Gardens
14          The Grand Doubletree Shops                     Downtown Miami
15                 Las Olas Riverfront                    Fort Lauderdale
16                      Las Olas Shops                    Fort Lauderdale
17                   Lincoln Road Mall                        Miami Beach
18           Loehmann's Fashion Island                           Aventura
19                Mall of the Americas                              Miami
20            The Mall at 163rd Street                  North Miami Beach
21        The Mall at Wellington Green                         Wellington
22            Miami International Mall                              Doral
23                 Miracle Marketplace                              Miami
24              Metrofare Shops & Cafe  Government Center, Downtown Miami
25                 Pembroke Lakes Mall                     Pembroke Pines
26                 Pompano Citi Centre                      Pompano Beach
27                      Sawgrass Mills                            Sunrise
28                   Seminole Paradise                          Hollywood
29          The Shops at Fontainebleau                        Miami Beach
30  The Shops at Mary Brickell Village                    Brickell, Miami
31          The Shops at Midtown Miami                      Midtown Miami
32       The Shops at Pembroke Gardens                     Pembroke Pines
33           The Shops at Sunset Place                        South Miami
34                      Southland Mall                         Cutler Bay
35           Town Center at Boca Raton                         Boca Raton
36      The Village at Gulfstream Park                   Hallandale Beach
37             Village of Merrick Park                       Coral Gables
38                   Westfield Broward                         Plantation
39                       Westland Mall                            Hialeah