0

I am using the popular R library "quantmod" and cannot figure out how to keep the character row names when I read the file to a csv.

For instance, I will have some data as follows:

ROW NAME       VALUE
1970-05-08     .05
1970-08-01     .05
1970-12-10     .06
...            ---

When I use

write.csv(MyData,'MyData.csv', row.names = T)

The output looks like this:

Column One     Column Two
1               .05
2               .05
3               .06
...             ---

How do I keep the character row name? I.e. how can value 1 in the csv actually read as 1970-05-08?

Thanks!

HonnSolo
  • 165
  • 11
  • 1
    can you add dput(MyData) to your post? the answer depends on what exactly MyData is – MichaelChirico Feb 17 '18 at 17:25
  • at a glance, probably you are looking for the saveSymbols function: https://stackoverflow.com/questions/28613295/download-save-load-roundtrip-with-quantmod-in-r – MichaelChirico Feb 17 '18 at 17:26
  • 1
    I can't understand from your question title and body whether you're trying to read a CSV, or write one. Consider `read.csv.zoo()` and/or `write.zoo()`. – Joshua Ulrich Feb 17 '18 at 17:49
  • I am writing a csv. The data "MyData" shown above with the row names, is being pulled in from yahoo finance. I am trying to write that data to a csv, which works but I want to preserve the text from the row name, and not have it converted to a number. – HonnSolo Feb 17 '18 at 19:03

2 Answers2

1

You have to be aware that quantmod returns xts-objects. So the first column (the dates) is the index. To write xts-objects to a, say csv-file, the easiest way is to use the write.zoo function.

getSymbols('AAPL',from='2018-01-01’)
write.zoo(AAPL,'aapl.csv',sep=',',quote=FALSE)

quotes=FALSE removes the quotes around the column names.

hvollmeier
  • 2,956
  • 1
  • 12
  • 17
0

Overview

You'll need to store the desired character row names as a new column in your data frame prior to exporting it as a .csv file.

To avoid a redundant column, set row.names = FALSE inside the write.csv() function. When importing the data frame back into , set row.names in read.csv() to the name of the column which represents your desired row name values .

Reproducible Example

Here, the row names are a character, representing the movie in which each Star Wars character dies.

# create data
df <-
  data.frame( ID = 1:3
              , Name = c( "Anakin", "Han", "Luke" )
              , stringsAsFactors = FALSE
  )

# create row names
rownames( df ) <- c( "Episode_VI", "Episode_VII", "Episode_VIII" )

# view data
df
#              ID   Name
# Episode_VI    1 Anakin
# Episode_VII   2    Han
# Episode_VIII  3   Luke

# store rownames in new column
df$row.names <- rownames( df )

# export data as CSV
write.csv( x = df
           , row.names = FALSE
           , file = "Star_Wars_data.csv"
)

# import data from CSV
df.csv <- read.csv( file = "Star_Wars_data.csv"
                , header = TRUE
                , row.names = "row.names"
                , stringsAsFactors = FALSE
)

# view data from CSV
df.csv
#              ID   Name
# Episode_VI    1 Anakin
# Episode_VII   2    Han
# Episode_VIII  3   Luke

# end of script #

Save as RDS File

Also, you can use saveRDS() to export your data frame as an .rds file. This method does not require you to store the row names as a column; rather, it simply saves is when importing into using readRDS().

# create data
df <-
  data.frame( ID = 1:3
              , Name = c( "Anakin", "Han", "Luke" )
              , stringsAsFactors = FALSE
  )

# create row names
rownames( df ) <- c( "Episode_VI", "Episode_VII", "Episode_VIII" )

# view data
df
#              ID   Name
# Episode_VI    1 Anakin
# Episode_VII   2    Han
# Episode_VIII  3   Luke

# Export as RDS file
saveRDS( object = df
         , file = "Star_Wars_data.rds"
)

# view data
readRDS( file = "Star_Wars_data.rds" )
#              ID   Name
# Episode_VI    1 Anakin
# Episode_VII   2    Han
# Episode_VIII  3   Luke

# end of script #
Cristian E. Nuno
  • 2,822
  • 2
  • 19
  • 33