Replacing data in .Rdata file - r

Is there a way I could replace the table in .Rdata file with another one? I can edit it with edit(x) command, but it would take an enormous amount of time to do this manually; besides, I haven't found a way to add rows to it.

I think you need to read a few 'intro to R' guides.
A .Rdata file is generally a saved session, and can have any number of 'things' saved in it, scalars, vectors, data.frames, lists, functions etc etc. I assume you have a data file that has been read into R into a data.frame and that is saved within a .Rdata file. You can load the .Rdata file with load("....Rdata") then you can 'replace' your table (a data frame), by loading another one over top, if that's what you want to do, so assuming it's called dat, dat <- read.csv("new_data.csv", ...), and then save the session again, save.image("....Rdata"). I've assumed a lot of things there though...

Related

How to delete temporary files of deleted objects in R package raster

Since I work with large RasterBrick objects, the objects can't be stored in memory but are stored as temporary files in the current Temporary files directory tempdir() or to be exact in the subfolder "raster".
Due to the large file sizes it would be very nice to delete the temporary files of the unused objects.
If I delete objects I no longer need by
rm(list=ls(pattern="xxx")
the temporary files still exist.
Garbage collection gc() will have no effect on that to my understanding since it has no effect on the hard drive.
The automatically given names of the temporary files don't show any relation to the object names.
Therefore it is not possible to delete them by a code like
raster_temp_dir <- paste(tempdir(), "/raster", sep="")
files_to_be_removed <- list.files(rastertemp, pattern="xxx", full.names=T)
Unfortunately the files of objects still in use aren't read-only.
Therefore I also would delete objects I still need by running:
files_to_be_removed <- list.files(rastertemp, full.names=T)
Did somebody already solve this problem or has any ideas how to solve it?
It would be perfect if somehow, there a code could distinguish between unused and used objects.
Since this is unlikely to implement a detour could be naming the temporary files of the Raster objects manually, but I haven't encountered an option for this neither since the filename argument can only be used when writing files to the hard disk but not when temporary files are created (to my knowledge).
Thanks!
I think the function you're looking for is file.remove(). Just pass it a vector with the file names you want to delete.

How to keep style format unchanged after writing data using openxlsx in R

I am using openxlsx in order to write the outputs of my data.
I have used the following code to read my data using readxl.
df1=read_excel("C:/my_data.xlsx",skip=2);
Now I want to write the output and keep the original Excel file using any possible package. I have used the following codes, but it does not keep the original Excel file. Can we do it it in R packages?
write.xlsx(df1, 'C:/mydata.xlsx',skip=2)
Given your code, you should nhave two different data files in your working directory:
"my_data.xlsx" (the one that you loaded), and "mydata.xlsx" (the one that you created through R). R shouldn't overwrite your files if you give them different names.
If there's only one file, are you sure you didn't use the same name for both files? If so, then everything should work fine if you give the files different names (e.g. "my_file1.xlsx" and "my_file2.xlsx")!
Also, in general, it's a good idea to give data files an informative name so that you don't accidentally delete/overwrite files that you need. For example, if the original excel data is you raw data, consider naming it "data_raw.xlsx", and make sure that you only read it, and whenever you make some changes to it, save it under a different name (e.g. "data_processed1.xlsx").
You can also save data files in the native R format .rds using the save_rds() function, this is especially helpful if you want to keep special attributes of variables such as factors, etc...
Hope this helps!

How to output a list of dataframes, which is able to be used by another user

I have a list whose elements are several dataframes, which looks like this
Because it is hard for another user to use these data by re-running my original code. Hence, I would like to export it. As the graph shows, the dataframes in that list have different number of rows. I am wondering if there is any method to export it as file without damaging any information, and make it be able to be used by Rstudio. I have tried to save it as RData, but I don't know how to save the information.
Thanks a lot
To output objects in R, here are 4 common methods:
dput() writes a text representation of an R object
This is very convenient if you want to allow someone to get your object by copying and pasting text (for instance on this site), without having to email or upload and download a file. The downside however is that the output is long and re-reading the object into R (simply by assigning the copied text to an object) can hang R for large objects. This works best to create reproducible examples. For a list of data frames, this would not be a very good option.
You can print an object to a .csv, .xlsx, etc. file with write.table(), write.csv(), readr::write_csv(), xlsx::write.xlsx(), etc.
While the file can then be used by other software (and re-imported into R with read.csv(), readr::read_csv(), readxl::read_excel(), etc.), the data can be transformed in the process and some objects cannot be printed in a single file without prior modifications. So this is not ideal in your case either.
save.image() saves your entire workspace (objects + environment)
The workspace can then be recreated with load(). This can be useful, but you are here only interested in saving one object. In that case, it is preferable to use:
saveRDS() which allows to write one object to file
The object can then be re-created with readRDS(). This is the best option to save an R object to file, without any modification and then re-create it.
In your situation, this is definitely the best solution.

What are the commands for viewing a ".RData" file's data in RStudio?

I am trying to find out how I can see the data within a dataset with a .RData extension.
I tried view(), it gave me one object present in the dataset but I know that this dataset is a large dataset with over 300MB size and consists of a very large number of names list. I need to view all of the contents of it and have been unsuccessful so far.
Should I convert it into a CSV instead in order to view all of the contents? If yes, how can I do that using RStudio?
The cross-platform function is View. (Caps are discriminatory in R.) If you did:
obj <- load("filename.Rdata") # assuming a file exist in your working directory
Then type:
obj
You should see a print-listing of the character representations of the objects created (or possibly overwritten) in your global environment. The Rstudio aspect of this question would not affect the result.

Command to use with easy way the insert of R dataframe

I have a dataframe loaded successfully in R.
I would like to give the data of df to someone else to use them with quick and easy way without need to load again the file into a df.
Which is the command to give the whole data of df (not the str())
You can save the file into a .RData using save or save.image, depending on your needs. First one will save specific objects while the latter will dump the whole workspace to a file. This method has the advantage of working on probably any R object.
Another option is as #user1945827 mentioned, using dput which will produce a string that is parseable into another R session. This will not work for complex (like S4) objects.

Resources