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

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.

Related

Converting RData to CSV file returns incorrect CSV file

I do not have any expertise on R and I have to convert RData files to CSV to analyze the data. I followed the following links to do this: Converting Rdata files to CSV and "filename.rdata" file Exploring and Converting to CSV. The second option seemed to be a simpler as I failed to understand the first one. This is what I have tried till now and the results along with it:
>ddata <- load("input_data.RData")
>print(ddata)
[1] "input_data"
> print(ddata[[1]])
[1] "input_data"
> write.csv(ddata,"test.csv")
From the first link I learnt that we can see the RData type and when I did str(ddata) I found out that it is a List of size 1. Hence, I checked to see if print(ddata[[1]]) would print anything apart from just "input_data". With the write.csv I was able to write it to a csv without any errors but it has just the following 2 lines inside the CSV file:
"","x"
"1","input_data"
Can you please help me understand what am I doing wrong and show a way to get all the details in a csv?
The object ddata contains the name of the object(s) that load() created. Try typing the command ls(). That should give you the names of the objects in your environment. One of them should be input_data. That is the object. If it is a data frame (str(input_data)), you can create the csv file with
write.csv(input_data, "test.csv")

Convert SPSS file to CSV and read to create Data Table

I have an SPSS file from which I am creating a data table using R script. But the problem is it's taking a bit of time to load the data table. I want to convert the SPSS(.sav) file into CSV file first and then read the CSV file to create a data table. So far I have tried multiple codes but that didn't work out properly.
Here's the code which I got from this.
I think foreign package in r can be used to solve this problem.
library(foreign)
write.table(read.spss("inFile.sav"), file="outFile.csv", quote = TRUE, sep = ",")

Append new lines to a .Rda file in 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

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.

how to write multiple dataframe to multiple sheet of one csv excel file in R?

I am trying to write multiple dataframe to a single csv formated file but each in a different sheet of the excel file:
write.csv(dataframe1, file = "file1.csv",row.names=FALSE)
write.csv(dataframe2, file = "file2.csv",row.names=FALSE)
is there any way to specify the sheet along with the csv file in this code and write them all in one file?
thank you in advance,
This is not possible. That is the functionality of csv to be just in one sheet so that you can view it either from notepad or any other such software. If you still try to write it would get over ridden. Just try to open a csv and open a new sheet and just write some values and save it. The values which were already there is erased. one excel file in csv format can have only one sheet.
The xlsx and XLConnect packages will do the trick as well.

Resources