How do I export a data frame to Excel? - r

I am trying to export a dataframe from R to excel. I am using the 'writexl' package but it does not seem to work.
The code is as following:
install.packages('writexl')
library(writexl)
write_xlsx(data_frame, "H:\\folder1.xlsx")
There does not seem to be any error produced and the code appears to have run, however when I look in 'folder1' the data_frame is not there.
Is there anything I am doing incorrectly?

I've found the openxlsx package to be easier to use than the xlsx package. It also doesn't have a java dependency. The main command for directly writing a data frame to an Excel file is write.xlsx. You can also create worksheets, do lots of fancy formatting and write multiple tables to a worksheet (see the vignettes here for some examples), but start with write.xlsx for the basic creation of Excel files.

Related

Exporting dataframe to an existing excel without using xlsx package R

I need to export multiple dataframes to an existing excel file. I'm working in AWS and as stated in this post Can't upload xlsx library in Amazon Web Service, I can't use write.xlsx from the xlsx package.
Similar functions such as write_xlsx do not have the append function that allows me to export the dataframes in the existing file without overwriting it. Which function could be a substitute for write.xlsx? Thanks in advance! (Sorry for not providing any reproducible example, I didn't come up with any for this particular problem)

Optimum way to overwrite an xlsx worksheet

I'm trying to write an Excel worksheet with the XLConnect package. The data I'm using is a data.frame (820*132). Once I'm done building the dataset, I'm using the writeWorksheetToFile function to export.
If the file does not exist yet and I am creating it from scratch, everything works well.
If I want to overwrite an existing sheet, the function takes approximately a minute to write and in addition, when I open the excel file, I have an error message saying: "we found a problem with some content in 'my_file.xlsx. Do you want to try to recover as much as we can?"
I tried to use other packages to write to excel like xlsx and openxlsx but they do not allow to overwrite a sheet without overwriting the entire workbook.
I've checked a few solutions such as this, but I not optimal.
I am looking for the most optimal way of writing excel worksheets, with an overwrite option that is suitable for large datasets.
I'm using the latest versions of R and RStudio.
My Excel verion is 1902, 64bits.

Import R example dataset into excel

I am using the topmodel package in R with the huagrahuma dataset that comes with the package.
I would like to bring all these variables into excel, edit those as per my requirement & then use the base in R.
package & data: https://rdrr.io/cran/topmodel/man/huagrahuma.html
You can save datasets using a command like write.csv() or one of the writing functions from the readr, readxl or xlsx packages (for example). Using:
?write.csv()
Will show you how to use the function. Once you have it saved as a .csv file on your computer, you can open it with Excel and do what you need with it.
Edit: Following G5W's comment below, you could try extracting elements of this list and saving those, depending on what you actually want to change. To be honest, with a list structure, you are better off changing the data in R, using any of the apply family of functions, or the purrr package. R is much better than Excel for transforming/tidying data, so why not use it? :-)

Importing EndNote .enl file into R Dataframe

I would appreciate it if someone can help me import this EndNote .enl file into R Dataframe.
When I came across this issue I tried a number of different packages, including bibliometrix (which is a very extensive package but a bit too complicated for this task I thought), bibtex and RISmed, without success.
But then I found the RefManageR package which worked great for this.
I first exported the Endnote library to Bibtex, there are pretty good directions for that step at:
https://libguides.usask.ca/c.php?g=218034&p=1458583
Then I imported the file that I had just creating using the following code in R:
# Basic importing of BibTex files
library(RefManageR)
# User RefManageR to import bibtex references and convert to tibble
bib = ReadBib("MyEndnoteBibtexExportFile.bibtex")
d.bibEQL = as.data.frame(bibEQL)
(I wanted a data frame for some custom analysis and plotting, seems like once you are here you are flexible to do whatever you want with the data, since it's just a a standard R data structure. RefManageR also has functions for further processing if that's what you want.)

How to export a dataset to SPSS?

I want to export a dataset in the MASS package to SPSS for further investigation. I'm looking for the EuStockMarkets data set in the package.
As described in http://www.statmethods.net/input/exportingdata.html, I did:
library(foreign)
write.foreign(EuStockMarkets, "c:/mydata.txt", "c:/mydata.sps", package="SPSS")
I got a text file but the sps file is not a valid SPSS file. I'm really looking for a way to export the dataset to something that a SPSS can open.
As Thomas has mentioned in the comments, write.foreign doesn't generate native SPSS datafiles (.sav). What it does generate is the data in a comma delimited format (the .txt file) and a basic syntax file for reading that data into SPSS (the .sps file). The EuStockMarkets data object class is multivariate time series (mts) so when it's exported the metadata is lost and the resulting .sps file, lacking variable names, throws an error when you try to run it in SPSS. To get around this you can export it as a data frame instead:
write.foreign(as.data.frame(EuStockMarkets), "c:/mydata.txt", "c:/mydata.sps", package="SPSS")
Now you just need to open mydata.sps as a syntax file (NOT as a datafile) in SPSS and run it to read in the datafile.
Rather than exporting it, use the STATS GET R extension command. It will take a specified data frame from an R workspace/dataset and convert it into a Statistics dataset. You need the R Essentials for Statistics and the extension command, which are available via the SPSS Community site (www.ibm.com/developerworks/spssdevcentral)
I'm not trying to answer a question that has been answered. I just think there is something else to complement for other users looking for this.
On your SPSS window, you just need to find the first line of code and edit it. It should be something like this:
"file-name.txt"
You need to find the folder path where you're keeping your file:
"C:\Users\DELL\Google Drive\Folder-With-Your-File"
Then you just need to add this path to your file's name:
"C:\Users\DELL\Google Drive\Folder-With-Your-File\file-name.txt"
Otherwise SPSS will not recognize the .txt file.
Sorry if I'm repeating some information here, I just wanted to make it easier to understand.
I suppose that EuStockMarkets is a (labelled) data frame.
This should work and even keep the variable and value labels:
require(sjlabelled)
write_spss(EuStockMarkets, "mydata.sav")
Or you try rio:
rio::export(EuStockMarkets, "mydata.sav")

Resources