How to read .rdata file in R
Just semove the df <-
in your code.
load()
will load all the objects contained in the .RData file. This can be multiple variables and dataframes with various names, so you do not need to specify one object name.
How to extract code from .Rdata file?
You can try loading the RData
file, and listing its contents:
load("mydata.RData", verbose=TRUE)
Then, you can view the code behind the objects loaded. For instance, if you just loaded a function called myfunc
you could view the definition by just entering the function's name:
myfunc
Read dataframe from list in multiple RData files
Loop through filenames, load, then extract the dataframe, (not tested):
dfList <- lapply(my_files, function(i){
load(i)
Z$df
})
R: Unable to view data when importing RData file
Check out head(dfAllxY)
. The load
command doesn't return an object, it returns the names of all the objects it loaded from the RData file since RData file may contain more than one object. Read the ?load
help page for more information.
Extracting specific data from an RData file
after putting the rdata file in your working directory, do something like
load("C:/Users/1_adm.RData", ex <- new.env())
ls.str(ex)
The rdata loads variable into your workspace so that command will show you what the variable names are, so you can then use them.
I don't have the file you're talking about so I can't tell you how to access the specific fields you need.
edit
Based on your comment:
Ok so the object you load is an object of type SpatialPolygonsDataFrame, and that has a Slot/property called Polygons, with objects of type Polygon. Those polygons I assume are the lat lng coordinates around the country. Accessing the coordinates should look something like
ps <- gadm@polygons[1]
then something like
ps@coords
load .Rdata file into list()
Using load
inside mget
with other envir=
onment than the .GlobalEnv
.
d1 <- d2 <- d3 <- d4 <- data.frame()
save(d1, d2, d3, d4, file="test.rda")
rm(d1, d2, d3, d4)
x <- mget(load("test.rda", envir=(NE. <- new.env())), envir=NE.)
ls()
# [1] "NE." "x"
x
# $d1
# data frame with 0 columns and 0 rows
#
# $d2
# data frame with 0 columns and 0 rows
#
# $d3
# data frame with 0 columns and 0 rows
#
# $d4
# data frame with 0 columns and 0 rows
Programmatically extract an object from collection of RData files
We found a useful solution.
- An extractor function
extractorRData <- function(file, object) {
#' Function for extracting an object from a .RData file created by R's save() command
#' Inputs: RData file, object name
E <- new.env()
load(file=file, envir=E)
return(get(object, envir=E, inherits=F))
}
- Extract a data frame "allParams" from RData file "priorRun.RData" without loading the entire environment.
allParams.prior <- extractorRData("priorRun.RData", "allParams")
This approach has proven to be both fast, and flexible. Useful with large data frames that would be slow to reconstruct.
Related Topics
Can't Download Data from Yahoo Finance Using Quantmod in R
How to Define Fixed Aspect-Ratio for (Base R) Scatter-Plot
Filter Function in Dplyr Errors: Object 'Name' Not Found
How to Position Strip Labels in Facet_Wrap Like in Facet_Grid
Calculate Multiple Aggregations on Several Variables Using Lapply(.Sd, ...)
Rscript Does Not Load Methods Package, R Does -- Why, and What Are the Consequences
Apply a Function Over Groups of Columns
Get_Map Not Passing the API Key (Http Status Was '403 Forbidden')
R Error "Sum Not Meaningful for Factors"
How to Parse Year + Week Number in R
In R, Use Gsub to Remove All Punctuation Except Period
Create Zip File: Error Running Command " " Had Status 127
Re-Ordering Bars in R's Barplot()
There Is Pmin and Pmax Each Taking Na.Rm, Why No Psum
Output a Vector in R in the Same Format Used for Inputting It into R
Group Integer Vector into Consecutive Runs