can I write an xts object using write.csv in R
Yes you can, and the easiest way may be via write.zoo
:
R> write.zoo
function (x, file = "", index.name = "Index", row.names = FALSE,
col.names = NULL, ...)
{
if (is.null(col.names))
col.names <- !is.null(colnames(x))
dx <- as.data.frame(x)
stopifnot(all(names(dx) != index.name))
dx[[index.name]] <- index(x)
dx <- dx[, c(ncol(dx), 1:(ncol(dx) - 1))]
write.table(dx, file = file, row.names = row.names, col.names = col.names,
...)
}
<environment: namespace:zoo>
R>
and here is a complete example:
R> mat <- matrix(rnorm(20),5,4, dimnames=list(NULL, LETTERS[1:4]))
R> mat
A B C D
[1,] -2.5304768 0.5454043 0.754670 0.330617
[2,] -0.5199045 0.3943289 -1.271524 -2.243113
[3,] -0.0996277 -0.0513063 -0.846310 -0.140727
[4,] 0.3819981 0.5230709 1.131108 2.398311
[5,] 1.4366976 -1.7750772 0.193936 1.047754
R> xmat <- xts(mat, order.by=Sys.Date() + seq(-4,0))
R> xmat
A B C D
2012-01-19 -2.5304768 0.5454043 0.754670 0.330617
2012-01-20 -0.5199045 0.3943289 -1.271524 -2.243113
2012-01-21 -0.0996277 -0.0513063 -0.846310 -0.140727
2012-01-22 0.3819981 0.5230709 1.131108 2.398311
2012-01-23 1.4366976 -1.7750772 0.193936 1.047754
So now that we have our data, it is just a matter of writing it:
R> write.zoo(xmat, file="/tmp/demo.csv", sep=",")
R> system("cat /tmp/demo.csv")
"Index","A","B","C","D"
2012-01-19,-2.53047680387774,0.545404313269755,0.754669841541681,0.330616876246245
2012-01-20,-0.519904544868541,0.394328857686792,-1.27152367237311,-2.24311276135881
2012-01-21,-0.0996276931028331,-0.0513062656752562,-0.846309564748021,-0.14072731914499
2012-01-22,0.381998053276389,0.523070920853495,1.13110826400249,2.39831100812159
2012-01-23,1.43669757366164,-1.77507724264279,0.193935657150967,1.04775355172344
R>
Edit on 25 Jan 2012 Use row.names=FALSE
, not TRUE
to suppress double row names. And as row.names=FALSE
is the default, remove it from the call.
export XTS to CSV, with separate columns
You need sep=","
.
Directly from the help for write.zoo
:
read.zoo and write.zoo are convenience functions for reading and writing "zoo"
series from/to text files. They are convenience interfaces to read.table and
write.table, respectively.
... further arguments passed to read.table, write.table, or read.zoo, respectively.
Hence, this should work:
write.zoo(dbt,file="export.csv",index.name="okay",row.names=FALSE,col.names=TRUE,sep=",")
Write xts/zoo object to csv with index
The date does show up. Here is a reproducible example:
dfA = read.table(textConnection('row.name HAM1 HAM2 HAM3 HAM4 HAM5 HAM6 "EDHEC LS EQ" SP500 "TR US 10Y" "TR US 3m TR"
1996-01-31 0.0074 NA 0.0349 0.0222 NA NA NA 0.034000 0.00380 0.00456
1996-02-29 0.0193 NA 0.0351 0.0195 NA NA NA 0.009300 -0.03532 0.00398
1996-03-31 0.0155 NA 0.0258 -0.0098 NA NA NA 0.009600 -0.01057 0.00371
1996-04-30 -0.0091 NA 0.0449 0.0236 NA NA NA 0.014700 -0.01739 0.00428
1996-05-31 0.0076 NA 0.0353 0.0028 NA NA NA 0.025800 -0.00543 0.00443
1996-06-30 -0.0039 NA -0.0303 -0.0019 NA NA NA 0.003800 0.01507 0.00412
1996-07-31 -0.0231 NA -0.0337 -0.0446 NA NA NA -0.044200 -0.00100 0.00454
1996-08-31 0.0395 -0.0001 0.0461 0.0351 NA NA NA 0.021100 -0.00448 0.00451
1996-09-30 0.0147 0.1002 0.0653 0.0757 NA NA NA 0.056300 0.02229 0.00470
1996-10-31 0.0288 0.0338 0.0395 -0.0180 NA NA NA 0.027600 0.02869 0.00428'), header = TRUE)
row.names(dfA) = as.Date(dfA$row.name, format = '%Y-%m-%d')
dfA$row.name = NULL
write.csv(dfA, file = 'delete.txt', row.names = TRUE)
Update
zoo
will lead to similar handling:
library(zoo)
zooA = as.zoo(dfA, order.by = row.names(dfA))
write.csv(zooA, file = 'delete.txt', row.names = TRUE)
"","HAM1","HAM2","HAM3","HAM4","HAM5","HAM6","EDHEC.LS.EQ","SP500","TR.US.10Y","TR.US.3m.TR"
"1996-01-31",0.0074,NA,0.0349,0.0222,NA,NA,NA,0.034,0.0038,0.00456
"1996-02-29",0.0193,NA,0.0351,0.0195,NA,NA,NA,0.0093,-0.03532,0.00398
"1996-03-31",0.0155,NA,0.0258,-0.0098,NA,NA,NA,0.0096,-0.01057,0.00371
"1996-04-30",-0.0091,NA,0.0449,0.0236,NA,NA,NA,0.0147,-0.01739,0.00428
"1996-05-31",0.0076,NA,0.0353,0.0028,NA,NA,NA,0.0258,-0.00543,0.00443
"1996-06-30",-0.0039,NA,-0.0303,-0.0019,NA,NA,NA,0.0038,0.01507,0.00412
"1996-07-31",-0.0231,NA,-0.0337,-0.0446,NA,NA,NA,-0.0442,-0.001,0.00454
"1996-08-31",0.0395,-1e-04,0.0461,0.0351,NA,NA,NA,0.0211,-0.00448,0.00451
"1996-09-30",0.0147,0.1002,0.0653,0.0757,NA,NA,NA,0.0563,0.02229,0.0047
"1996-10-31",0.0288,0.0338,0.0395,-0.018,NA,NA,NA,0.0276,0.02869,0.00428
Update 2
Turns out that OP has an xts
object that has anindex
attribute rather than a rownames
attribute, which can be written out using a call to write.zoo
rather than write.csv
(which looks for rownames
).
write list of xts objects to excel
If you're going to read your list of xts objects into Excel, you probably need to convert it to a flat file. The code below transforms each xts
object into a data frame with each row containing the xts
object name, and its dates and values. The map_dfr
function from the purrr
package in tidyverse
is used to loop over the xts
objects in the list and combine the results into a single data frame.
library(xts)
library(tidyverse)
#
# combine list of xts objects into a single data frame with equity names and dates
#
df_out <- map_dfr(blowup.instances, function(y) data_frame(Name = names(y), Date = index(y), value=as.vector(coredata(y))) )
#
# write as csv flat file
#
write.csv(df_out, file="blowupInstances.csv", row.names = FALSE)
The data frame written to the file is
df_out
# A tibble: 4 x 3
Name Date value
<chr> <date> <dbl>
1 AERI.US.Equity 2018-06-27 -0.5
2 SRPT.US.Equity 2018-06-26 -0.64
3 SRPT.US.Equity 2018-06-27 -0.55
4 PTCT.US.Equity 2018-06-20 -0.7
where the data a simple example set I made.
Write ZOO (XTS) object into CSV file by separating index column into two columns
y <- data.frame(
date=format(index(x), '%Y-%m-%d'),
time=format(index(x), '%H:%M:%S'),
x=as.numeric(x)
)
write.csv(y, ...)
Can I write xts objects to excel including the row names
Consider changing the zoo
object to a data.frame
with first column as row names and then writeData
library(zoo)
library(openxlsx)
tempX <- fortify.zoo(tempX)
writeData(wb, sheet = outputName, tempX, rowNames = FALSE, keepNA = TRUE)
saveWorkbook(wb, outputFileName, overwrite = TRUE)
-output
R: Export specific value class from xts objects
Found a solution. Didn't recognize that value classes in xts objects can be called like columns in a data frame.
Used the following loop to export the specific content from xts to csv.
for (n in c("vol1", "vol2", "vol3", "vol4", "vol5", "vol6", "vol7", "vol8")) {
v = get(n)
extr <- v[j = 'spot']
vola <- as.data.frame(extr)
myfile <- paste("Vola_Est_1Min_",n,".csv", sep="")
write.csv(vola, myfile)
}
Related Topics
R Dplyr Filter Based on Matching Search Term with First Words of Any Work in Select Columns
R Multiple Conditions in If Statement
Multiple Groups in Geom_Density() Plot
How to Hide/Toggle Legends Based on Addlayercontrol() in Leaflet for R
R: Is There a Good Replacement for Plyr::Rbind.Fill in Dplyr
Combining Vector and Bitmap Graphics in a PDF
Outputing N Tables in Shiny, Where N Depends on the Data
Importing S3 Method from Another Package
Is There a Command Similar to Matlab's "Close All" in R? (How to Close All Graphics Devices)
Removing a Group of Words from a Character Vector
How to Force Seasonality from Auto.Arima
How to Make Join Operations in Dplyr Silent
Adjusting the Node Size in Igraph Using a Matrix
Tiny Plot Output from Sankeynetwork (Networkd3) in Firefox
Change Background Colour of Knitr::Kable Headers
Installing R Packages Error in Readrds(File):Error Reading from Connection