Tengo una serie de tiempo XTS en R del siguiente formato y estoy tratando de hacer algún procesamiento, subconjunto y reorganización antes de exportarlo como un CSV para trabajar en otro programa.¿Cómo puedo cambiar XTS a data.frame y mantener el índice en R?
head(master_1)
S_1
2010-03-03 00:00:00 2.8520
2010-03-03 00:30:00 2.6945
2010-03-03 01:00:00 2.5685
2010-03-03 01:30:00 2.3800
2010-03-03 02:00:00 2.2225
2010-03-03 02:30:00 2.0650
y
str(master_1)
An ‘xts’ object from 2010-03-03 to 2010-05-25 08:30:00 containing:
Data: num [1:4000, 1] 2.85 2.69 2.57 2.38 2.22 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr "S_1"
Indexed by objects of class: [POSIXt,POSIXct] TZ:
Original class: 'zoo'
xts Attributes:
List of 1
$ dateFormat: chr "Date"
Y me gustaría convertir esto en un hoja.de.datos para que pueda manipularlo más fácilmente y luego exportar a otro programa. Sin embargo, cuando uso test1 <- as.data.frame(master_1)
el test1 tiene el índice (es decir, las fechas y horas) visibles,
head(test1)
S_1
2010-03-03 00:00:00 2.8520
2010-03-03 00:30:00 2.6945
2010-03-03 01:00:00 2.5685
2010-03-03 01:30:00 2.3800
2010-03-03 02:00:00 2.2225
2010-03-03 02:30:00 2.0650
pero el índice no está demostrado,
str(test1)
'data.frame': 4000 obs. of 1 variable:
$ S_1: num 2.85 2.69 2.57 2.38 2.22 ...
y escribir un csv write.csv(master_1, file="master_1.csv")
no incluye la hora o la fecha. ¿Por qué es esto y cómo puedo incluir los datos de datos/tiempo como una columna, por lo que se usa en otros comandos R y se exporta correctamente?
Gracias por cualquier ayuda.
Esto es correcto. Para responder a la segunda pregunta: 'write.csv' no incluye el índice porque el índice es un atributo xts *, no un nombre de fila. Use 'write.zoo' en su lugar. –
Gracias Shane y Joshua, eso me ayuda a ver dónde he estado yendo mal. – phrozenpenguin
'' data.frame (date = index (master_1), value = coredata (master_1) [,]) '' para cambiar el nombre de la variable de una vez. – PatrickT