how to remove index column when export file in R - r

I am using R programming to export a .dat file. but the exported file is always included the index column. Is there any possible way/ code to export data in R without index column?

Lets say you are using write.table and your dataset is df then following will work for you
write.table(df,'df.dat',row.names=F)

Related

R export a data frame into a .gsheet file

I'm currently exporting my DataFrame into a .csv file with the following command
write.csv(MyDataFrame,file="MyFile.csv")
and desire to convert it so that it exports directly into a .gsheet file format (Google Sheet).
How may I achieve this?

Export elements of a list to separate excel files in R

I've got a list of 24 dataframes and I'm wondering if there's an easy way to export each element to its own excel file from R, preferably using the name of the list element in the excel files title?
I know I can do write.xlsx(listofdfs, "newexcelfilename.xlsx") and it will put each element into its own worksheet within the file, but in my case I'll eventually need to email these files separately so it is best if I can get them into their own individual excel file. Thanks in advance!
You may use any of the apply function in base R. For example, using Map -
Map(openxlsx::write.xlsx, listofdfs, paste0(names(listofdfs), '.xlsx'))
Also with purrr::imap -
purrr::imap(listofdfs, ~openxlsx::write.xlsx(.x, paste0(.y, '.xlsx')))

Problem with export data from excel to R (not interpreting data as numbers)

I have the following problem with exporting data from excel, and importing them into R.
My data in excel contains "," so excel interprets them as numbers.
But when I have this data in R I have "." so R interprets them as text instead of numbers.
For example,
In excel I have 12,765 and in R I have 12.765
Do you have any idea how can I fix this ?
I use the following code to import the data file into R:
library(openxlsx)
read.xlsx("pk.xlsx")
I suggest using the nice package readxl from the tidyverse (https://readxl.tidyverse.org/) :
library(readxl)
read_excel("pk.xlsx")
Your should find everything you need with the tidyverse to import different format of files (readr for all csv, tsv and so on... ).

how to refresh an excel file from within R?

I have some excel file with simple formulas like =SUM(A1:A3).
I need to import the file into R, but before that I need to refresh the formulas. Is there a way to refresh the file from within R? There are good packages for importing the data in a R dataframe (eg. the R xslx package) but I need to refresh my formulas first.
Any suggestions?
Thanks!
You should be able to do this with RDCOMClient:
library(RDCOMClient)
ex = COMCreate("Excel.Application")
book = ex$Workbooks()$Open("my_file.xlsx")
book$Worksheets("Sheet1")$Calculate() # if you have many sheets you could loop through them or use apply functions based on their actual names
book$Save()
book$Close()
Here's another thread on the underlying VBA

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