I have a parameter called country_name that reflects the name of a country I am interested in and which I change sometimes when I run my code, I would like my RDA file to reflect that name change once saved and loaded back into the environment.
Currently what happens is this:
Name the country:
country_name <- "Ireland"
Create a simple data frame:
x <- 10
vars_2_keep <- data.frame(x)
vars_2_keep
It contains x=10
x
1 10
I save it, renaming the data frame with the country name so that when I do this with a different country I will have country specific information:
save(vars_2_keep, file=paste("my_data_", country_name[1], ".rda", sep = ""))
I delete everything, and load it back in:
rm(list=ls())
load(file='my_data_Ireland.rda')
Unfortunately, in the environment instead of my data frame being called "my_data_Ireland", it is still called vars_2_keep.
How can I update the name of this data frame to my_data_country_name[1] (which in this example would be my_data_Ireland)
Thank you
Related
I want to save data into an .RData file.
For instance, I'd like to save into 1.RData with two csv files and some information.
Here, I have two csv files
1) file_1.csv contains object city[[1]]
2) file_2.csv contains object city[[2]]
and additionally save other values, country and population as follows.
So, I guess I need to make objects 'city' from two csv files first of all.
The structure of 1.RData may looks like this:
> data = load("1.RData")
> data
[1] "city" "country" "population"
> city
[[1]]
NEW YORK 1.1
SAN FRANCISCO 3.1
[[2]]
TEXAS 1.3
SEATTLE 1.4
> class(city)
[1] "list"
> country
[1] "east" "west" "north"
> class(country)
[1] "character"
> population
[1] 10 11 13 14
> class(population)
[1] "integer"
file_1.csv and file_2.csv have bunch of rows and columns.
How can I create this type of RData with csv files and values?
Alternatively, when you want to save individual R objects, I recommend using saveRDS.
You can save R objects using saveRDS, then load them into R with a new variable name using readRDS.
Example:
# Save the city object
saveRDS(city, "city.rds")
# ...
# Load the city object as city
city <- readRDS("city.rds")
# Or with a different name
city2 <- readRDS("city.rds")
But when you want to save many/all your objects in your workspace, use Manetheran's answer.
There are three ways to save objects from your R session:
Saving all objects in your R session:
The save.image() function will save all objects currently in your R session:
save.image(file="1.RData")
These objects can then be loaded back into a new R session using the load() function:
load(file="1.RData")
Saving some objects in your R session:
If you want to save some, but not all objects, you can use the save() function:
save(city, country, file="1.RData")
Again, these can be reloaded into another R session using the load() function:
load(file="1.RData")
Saving a single object
If you want to save a single object you can use the saveRDS() function:
saveRDS(city, file="city.rds")
saveRDS(country, file="country.rds")
You can load these into your R session using the readRDS() function, but you will need to assign the result into a the desired variable:
city <- readRDS("city.rds")
country <- readRDS("country.rds")
But this also means you can give these objects new variable names if needed (i.e. if those variables already exist in your new R session but contain different objects):
city_list <- readRDS("city.rds")
country_vector <- readRDS("country.rds")
Just to add an additional function should you need it. You can include a variable in the named location, for example a date identifier
date <- yyyymmdd
save(city, file=paste0("c:\\myuser\\somelocation\\",date,"_RData.Data")
This was you can always keep a check of when it was run
I need some help creating a dataset in R where each observation contains a latitude, longitude, and date. Right now, I have a list of roughly 2,000 files gridded by lat/long, and each file contains observations for one date. Ultimately, what I need to do, is combine all of these files into one file where each observation contains a date variable that is pulled from the name of its file.
So for instance, a file is named "MERRA2_400.tavg1_2d_flx_Nx.20120217.SUB.nc". I want all observations from that file to contain a date variable for 02/17/2012.
That "nc" extension describes a netCDF file, which can be read into R as follows:
library(RNetCDF)
setwd("~/Desktop/Thesis Data")
p1a<-"MERRA2_300.tavg1_2d_flx_Nx.20050101.SUB.nc"
pid<-open.nc(p1a)
dat<-read.nc(pid)
I know the ldply command can by useful for extracting and designating a new variable from the file name. But I need to create a loop that combines all the files in the 'Thesis Data' folder above (set as my wd), and gives them date variables in the process.
I have been attempting this using two separate loops. The first loop uploads files one by one, creates a date variable from the file name, and then resaves them into a new folder. The second loop concatenates all files in that new folder. I have had little luck with this strategy.
view[dat]
As you can hopefully see in this picture, which describes the data file uploaded above, each file contains a time variable, but that time variable has one observation, which is 690, in each file. So I could replace that variable with the date within the file name, or I could create a new variable - either works.
Any help would be much appreciated!
I do not have any experience working with .nc files, but what I think you need to do, in broad strokes, is this:
filenames <- list.files(path = ".") # Creates a character vector of all file names in working directory
Creating empty dataframe with column names:
final_data <- data.frame(matrix(ncol = ..., nrow = 0)) # enter number of columns you will have in the final dataset
colnames(final_data) <- c("...", "...", "...", ...) # create column names
For each filename, read in file, create date column and write as object in global environment:
for (i in filenames) {
pid<-open.nc(i)
dat<-read.nc(pid)
date <- ... # use regex to get your date from i and convert it into date
dat$date <- date
final_data <- rbind(final_data, dat)
}
I have data frame name MC_df, I need to get the list of some variables in MC_df to access through my function, so if I do manually by vari_names_MCdf <- list(MC_df$Age, MC_df$Year, MC_df$Education, MC_df$City, MC_df$Country), it works, but due to the large of data set, I write all of variables' names I want to include into csv file. Now I read csv file to get access all of variables, I got stuck. Here is data MC_df
MC_df <- data.frame(ID=c(1:5),Age=c(18,25,30,22,19),
Year=c(2003,2010,2008,2010,2015),
Education = c("<12","13-15",">15","<12",">15"),
City = c("SD","MS","LA","CV","SD"),
Country=c("US","CA","CA","CA","US"),
Group=c(rep("group",5)))
I do manually, just call list all of variables I want, it works
vari_names_MCdf <- list(MC_df$Age, MC_df$Year, MC_df$Education, MC_df$City, MC_df$Country)
vari_group_MCdf <- list(MC_df$Group,MC_df$Group,MC_df$Group,MC_df$Group,MC_df$Group)
Now I write variables into csv file, and read.csv file, it helps me for the futures, I need to edit in csv file, I don't need to access from MC_df
df <- data.frame(names=c("Age","Year","Education","City","Country"),
text=c("Age Continuos","Year Category","Eduaction Group","City Category","Country Group"))
write.csv(df,"vari.csv")
var <- read.csv("vari.csv", stringsAsFactors = FALSE)
var
X names text
1 1 Age Age Continuos
2 2 Year Year Category
3 3 Education Eduaction Group
4 4 City City Category
5 5 Country Country Group
I try
vari_names <- as.list(var$names)
vari_text <- as.list(var$text)
get("MC_df")$vari_names[[1]]
vari_names_new <- lapply(vari_names, function(i) paste0("MC_df$",i, sep=""))
get(vari_names_new[[1]])
Everything doesn't work, so how I can active variable names in CSV file and dataset names
I wrote a script in R that merges and modifies some csv data and then saves the resulting data frame using write table(). When it saves the file it adds the current date to the name of the file. The third column of the resulting data frame is always country specific, so I was wondering if there is a way to include in the file name using write.table the name of the country depending on the country code (name of the third column).
For example, if the name of the third column is "it", I want to add "Italy" to the name of the csv file using write.table.
Import list of country names and codes into R: (It would be wise to do this at the very top of your script: outside your processing loop so you dont read in the data over and over for each dataset being written out to .csv. The rest of the code goes just before your current write.table command
library(RCurl)
csv_src <- getURL("https://raw.githubusercontent.com/umpirsky/country-list/master/country/cldr/en/country.csv")
world <- read.csv(text=csv_src, header=T)`
Get name of third column in your data with country codes:
countrycode <- colnames(yourdata)[3]
Extract corresponding country name:
country_idx <- grep(pattern=countrycode, x=world$iso, ignore.case = TRUE)
country <- world$name[country_idx]
Attach country name to csv filename (Replace "..." with whatever other tags you want appended to the output filename. Otherwise remove "...")
csv_name <- paste0("...",country, ".csv")
Write out your data to file:
write.table(x=yourdata, file=csv_name)
Good luck :-)
I want to save data into an .RData file.
For instance, I'd like to save into 1.RData with two csv files and some information.
Here, I have two csv files
1) file_1.csv contains object city[[1]]
2) file_2.csv contains object city[[2]]
and additionally save other values, country and population as follows.
So, I guess I need to make objects 'city' from two csv files first of all.
The structure of 1.RData may looks like this:
> data = load("1.RData")
> data
[1] "city" "country" "population"
> city
[[1]]
NEW YORK 1.1
SAN FRANCISCO 3.1
[[2]]
TEXAS 1.3
SEATTLE 1.4
> class(city)
[1] "list"
> country
[1] "east" "west" "north"
> class(country)
[1] "character"
> population
[1] 10 11 13 14
> class(population)
[1] "integer"
file_1.csv and file_2.csv have bunch of rows and columns.
How can I create this type of RData with csv files and values?
Alternatively, when you want to save individual R objects, I recommend using saveRDS.
You can save R objects using saveRDS, then load them into R with a new variable name using readRDS.
Example:
# Save the city object
saveRDS(city, "city.rds")
# ...
# Load the city object as city
city <- readRDS("city.rds")
# Or with a different name
city2 <- readRDS("city.rds")
But when you want to save many/all your objects in your workspace, use Manetheran's answer.
There are three ways to save objects from your R session:
Saving all objects in your R session:
The save.image() function will save all objects currently in your R session:
save.image(file="1.RData")
These objects can then be loaded back into a new R session using the load() function:
load(file="1.RData")
Saving some objects in your R session:
If you want to save some, but not all objects, you can use the save() function:
save(city, country, file="1.RData")
Again, these can be reloaded into another R session using the load() function:
load(file="1.RData")
Saving a single object
If you want to save a single object you can use the saveRDS() function:
saveRDS(city, file="city.rds")
saveRDS(country, file="country.rds")
You can load these into your R session using the readRDS() function, but you will need to assign the result into a the desired variable:
city <- readRDS("city.rds")
country <- readRDS("country.rds")
But this also means you can give these objects new variable names if needed (i.e. if those variables already exist in your new R session but contain different objects):
city_list <- readRDS("city.rds")
country_vector <- readRDS("country.rds")
Just to add an additional function should you need it. You can include a variable in the named location, for example a date identifier
date <- yyyymmdd
save(city, file=paste0("c:\\myuser\\somelocation\\",date,"_RData.Data")
This was you can always keep a check of when it was run