bvowe
bvowe

Reputation: 3384

Text File with Matrix

Desired which I hope to achieve:

hello number
A 1 1
B 1 1

This is code

m2=matrix(1,2,2)
row.names(m2)=c("A","B")
fileConn <- file("name.txt")
  writeLines(c(paste0("hello number"),
  c(paste(m2))), fileConn)
  close(fileConn)

But I get

hello number
1
1
1
1

So I am wondering how I can fix this for desired output? It is possible to do so and have row names for this matrix? Thanks a bunch!

Upvotes: 0

Views: 29

Answers (1)

Julian_Hn
Julian_Hn

Reputation: 2141

I'll add a complete answer here, since the comments only allow oneliners:

file <-  "name.txt"
writeLines("hello number",con=file)
write.table(m2,file=file,append=T,col.names=F)
write("goodbye number", file=file,append=T)

Upvotes: 1

Related Questions