HonnSolo
HonnSolo

Reputation: 165

Read CSV in R, but Keep Character Row Names

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!

Upvotes: 0

Views: 2090

Answers (2)

hvollmeier
hvollmeier

Reputation: 2986

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.

Upvotes: 1

Cristian E. Nuno
Cristian E. Nuno

Reputation: 2920

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 #

Upvotes: 1

Related Questions