Sometimes you may want to export your data from R (.Rdata) to another format, such as TXT file (a tab-delimited text file) and CSV file (comma separated values file). However, most used statistical software are SAS, Stata, and SPSS, so here we will show how you to export data to several formats.
In the code below, dt is the name of data in R, and mydata new data name.
(1) Exporting data to TXT (Tab Delimited Text File):
write.table(dt, "mydata.txt", sep=",")
(2) Exporting data to CSV:
write.table(dt, file="mydata.csv",sep=",",row.names=F)
(3) Exporting data to SPSS. Here is necessary to install foreing package:
require(foreign) write.foreign(dt, "mydata.txt", "mydata.sps", package="SPSS")
(4) Exporting data to Stata:
require(foreign) write.dta(dt, "mydata.dta")
(5) Export data to SAS:
library(foreign) write.foreign(dt, "mydata.txt", "mydata.sas", package="SAS")
Hope you find this short post useful.
Hi, I need your help.
here is my code :
library(stsm)
pars <- c(var1 = 1, var2 = 0.0001,var3 = 0.05, var4 = 0.08)
m <- stsm.model(model = "local-level", y = ts(seq(200), frequency = 52),
pars = pars, nopars = NULL)
ss <- char2numeric(m)
set.seed(123)
y <- datagen.stsm(n = 144, model = list(Z = ss$Z, T = ss$T, H = ss$H, Q = ss$Q),
n0 = 20, freq = 52, old.version = TRUE)$data
print(y)
my question is, how to tabulate the data into one column only or export the data to txt or csv file?
Thank you.
fantastic. Thanks
JK I figured it out apologies
I have some stock price data required from the “quantmod” package, however, when I export it i do not get the dates only the values. How could you solve this? im using write.foreign and exporting it to sps
You get values in SPSS, or in R?
I have the data in r that looks like this
2007-01-01 -0.07
2007-01-02 -0.06
from the code
require(quantmod)
SP <- getSymbols("^GSPC", from=as.Date("2007-01-01"), to=as.Date("2016-01-01"), auto.assign=FALSE)
SP$p <- log(SP$GSPC.Adjusted) ## log() computes the natural logaritm
SP$r <- SP$p – lag(SP$p) ## lag() computes the lag 1
But when I export it to txt and sps it looks like this
-0.07
-0.04… etc
So only the values and not the date label is extracted
When I transport from R to SPSS with using write.foreign i get values instead of dates in SPSS. I can simply change that by going to Variable View, and change the type from numeric to data in SPSS.