hedgedandlevered
hedgedandlevered

Reputation: 2394

data frames, input and output with files

I'm looking for a simple way to read/write a basic data frame:

symbMat <- matrix("AJY", "6A", "6J", 0, 0, ncol=5)
colnames(symbMat) <- c("Symb1","Symb2","Symb3","lastVstamp","completedRows")
testFrame<-List(symbMat,matrix(c("date1","date2")))

so testFrame now looks like

[[1]]
 Symb1 Symb2 Symb3 lastVstamp completedRows
[1,] "AJY" "6A"  "6J"  "0"        "0"          

[[2]]
     [,1]   
[1,] "date1"
[2,] "date2" 

so the problem is that read.table("MultiTable", header=TRUE) gives me a file that treats it all as one 2x6 matrix (with a messed up name for the last column)

"Symb1" "Symb2" "Symb3" "lastVstamp" "completedRows" "structure.c..date1....date2.....Dim...c.2L..1L.."
"1" "AJY" "6A" "6J" "0" "0" "date1"
"2" "AJY" "6A" "6J" "0" "0" "date2"

while

 install.packages("MASS")
 library(MASS)
 write.matrix(testFrame,file="MultiTable")

just makes a file

AJY, 6A , 6J , 0 , 0
date1, date2

which is obviously quite a bit of information loss. Besides, there isn't a read.matrix() so I don't know what the point of that function is anyway

Any ideas?

Upvotes: 0

Views: 82

Answers (1)

Brian Diggs
Brian Diggs

Reputation: 58835

First, testFrame is not a data.frame. A data.frame is a very specific object type in R. However, that does not impact being able to save and re-load data. There are two sets of functions for saving and loading data: save()/load() and dump()/source(). The first creates a binary format, the second a text format (though not structured the same as what is printed on screen).

Fixing the typos in your example:

symbMat <- matrix(c("AJY", "6A", "6J", 0, 0), ncol=5)
colnames(symbMat) <- c("Symb1","Symb2","Symb3","lastVstamp","completedRows")
testFrame<-list(symbMat,matrix(c("date1","date2")))

testFrame is

> testFrame
[[1]]
     Symb1 Symb2 Symb3 lastVstamp completedRows
[1,] "AJY" "6A"  "6J"  "0"        "0"          

[[2]]
     [,1]   
[1,] "date1"
[2,] "date2"

With save() and load():

save(testFrame, file="temp.RData")
load(file="temp.RData")

With dump() and source():

dump("testFrame", file="temp.R")
source(file="temp.R")

Note the first argument of dump() is quoted. temp.R is then

testFrame <-
list(structure(c("AJY", "6A", "6J", "0", "0"), .Dim = c(1L, 5L
), .Dimnames = list(NULL, c("Symb1", "Symb2", "Symb3", "lastVstamp", 
"completedRows"))), structure(c("date1", "date2"), .Dim = c(2L, 
1L)))

which represents the object the same way as dput() does.

Upvotes: 1

Related Questions