Append new lines to a .Rda file in R - r

Writing a fresh .Rda file to save a data.frame is easy:
df <- data.frame(a=c(1,2,3,4), b=c(5,6,7,8))
save(df,file="data.Rda")
But is it possible to write more data afterwards, there is no append=TRUE option using save.
Similarly, writing new lines to a text file is easy using:
write.table(df, file = 'data.txt', append=T)
However for large data.frames, the resulting file is much larger.

If you use Microsoft R, you might want to check RevoScaler package, rxImport function in particular. It allows you to store compressed data.frame in file, it also allows you to append new lines to existing file without loading it into environment.
Hope this helps. Link on function documentation below.
https://learn.microsoft.com/en-us/machine-learning-server/r-reference/revoscaler/rximport

Related

How to write table on Juliabox?

I define a DataFrame named data and want to write it into .csv file. I used writetable("result_data.csv", data) but it doesn't work.
This is the dataframe
error details
To write a data frame to a disk you should use the CSV.jl package like this (also make sure that you have write right to the directory you want to save the file on Juliabox):
using CSV
CSV.write("result_data.csv", data)
If this fails then please report back in the comment I will investigate it further.

Save text files with a different extension in r

I'm trying to write out an R output to a text file that is not saved as .txt but as some other unique identifier (for example .prt). I know that's possible with matlab, but I don't know how to get that to work with R.
I can't find any package to do that, and when I try to specify the extension in the file name it give me an error and doesn't save.
Any idea would be greatly welcome! Thank you.
Unless you are using some specialized package, a lot of standard R functions for writing data to files have a file= parameter (or similar) to let you specify whatever the filename (and extension) you want. For example:
dummy.data <- matrix(rnorm(25),ncol=5)
### in reality you could just write file="dummyfile.prt" as one string
### but for demonstration purposes, you can use paste0() or paste(,sep='')
### to create a new file name using some variable prefix and your extension
### ".prt"
### sep='\t' makes the output tab-delimited
write.table(dummy.data,file=paste0("dummyfile",".prt"),sep='\t')

Taking manipulated data out of the R console and creating a csv

I have used R to remove duplicates from a csv file using the following (lda_data is my csv file name)
unique(lda_data[duplicated(lda_data),])
This works great, however I need to get the results from the console into another csv file.
What are the methods of getting manipulated data from a csv file into another new and manipulated csv file?
Thanks in advance.
Use the write.csv command:
write.csv(dataframe, "/path/filename.csv", row.names = FALSE)
That should do the trick for you.

How do I export a data frame to a file?

How do I export a data frame from R (the file is in Global Environment) to some folder in desktop? I have created some data frames in R and need to export to the linux operating system. That's why I want to export the data frame to desktop/documents and later export to the Linux.
The Save function can do that, just specify the path you want to export the data. Change your directory in Rstudio to that folder in the desktop or else save it somewhere and do a cp linux command.
Save Function Information
Save Data Frame (Stack Overflow)
saving a data file in R
To write a data frame to a text file, the command is write.table. I'll let you read the help (?write.table) to see all the options, but a sample usage is
write.table(x = mtcars, file = "C:/exported_mtcars.txt")
This will write the data frame called mtcars to a file called exported_mtcars.txt on my C drive. The default is to use spaces to separate columns. If you want tab-separations, specify sep = "\t".
You may want to simply set your working directory to the folder you've created (setwd("C:/Users/...your filepath.../Desktop/your_folder")). Then you can just specify, e.g., file = "file1.txt" for the file names in write.table.
As far as writing multiple data frames to multiple files, I strongly recommend working with lists of data frames. I'll refer you to my answer here on the subject. See especially the section I didn't put my data in a list :( I will next time, but what can I do now?. You can then pretty easily use a for loop to save all your data frames to files using write.table.
You have to specify here the route where you want to export a complete image of your current working environment:
save.image("~/User/RstudioFiles/dataset.RData")
Hope it works!

Fetch data from an open excel sheet into R?

I am wondering is it possible to read an excel file that is currently open, and capture things you manually test into R?
I have an excel file opened (in Windows). In my excel, I have connected to a SSAS cube. And I do some manipulations using PivotTable Fields (like changing columns, rows, and filters) to understand the data. I would like to import some of the results I see in excel into R to create a report. (I mean without manually copy/paste the results into R or saving excel sheets to read them later). Is this a possible thing to do in R?
UPDATE
I was able to find an answer. Thanks to awesome package created by Andri Signorell.
library(DescTools)
fxls<-GetCurrXL()
tttt<-XLGetRange(header=TRUE)
I was able to find an answer. Thanks to awesome package created by Andri Signorell.
library(DescTools)
fxls<-GetCurrXL()
tttt<-XLGetRange(header=TRUE)
Copy the values you are interested in (in a single spread sheet at a time) to clipboard.
Then
dat = read.table('clipboard', header = TRUE, sep = "\t")
You can save the final excel spreadsheet as a csv file (comma separated).
Then use read.csv("filename") in R and go from there. Alternatively, you can use read.table("filename",sep=",") which is the more general version of read.csv(). For tab separated files, use sep="\t" and so forth.
I will assume this blog post will be useful: http://www.r-bloggers.com/a-million-ways-to-connect-r-and-excel/
In the R console, you can type
?read.table
for more information on the arguments and uses of this function. You can just repeat the same call in R after Excel sheet changes have been saved.

Resources