My intention is to format a simple matrix in csv format. As I need to further process the formatted lines, I don't want to write the formatted string to a file.
I already tried using textConnection, which seems to be the right approach.
m<-matrix(c(1,2,3,4), nrow=2)
result<-write.csv(m, file=textConnection(csvData), row.names=FALSE, col.names=FALSE)
I'm expecting csvData to contain the contents of the formatted csv (file) as a vector containing the lines.
I get the error:
Error in textConnection(csvData) : invalid 'text' argument
What is the proper usage of textConnection?
Revised question
After some trying and cleaning all variables I ended up with
rm(list = ls())
m<-matrix(c(1,2,3,4), nrow=2)
result<-write.csv(m, file=textConnection("csvData", "w"), row.names=FALSE, col.names=FALSE)
This produces at least no errors, but I ended with a warning that col.names are ignored. The content of csvData is also not what I expected
> csvData
[1] "\"V1\",\"V2\"" "1,3" "2,4"
How to remove the header?
My solution
After trying I found, that write.csv should be replaced by write.table.
rm(list = ls())
m<-matrix(c(1,2,3,4), nrow=2)
result<-write.table(m, file=textConnection("csvData", "w"), row.names=FALSE, col.names=FALSE, sep=";")
write.csv(m, "mymatrix.csv")generate the desired output? Check it viaread.csv("mymatrix.csv").write.csv(m, "mymatrix.csv", row.names=FALSE, col.names=FALSE)should do it, right?col.namesis ignored, so whywrite.csvhas a paramatercol.names, which is just ignored?