10

This is a very simple issue and I'm surprised that there are no examples online.

I have a vector:

vector <- c(1,1,1,1,1)

I would like to write this as a csv as a simple row:

write.csv(vector, file ="myfile.csv", row.names=FALSE)

When I open up the file I've just written, the csv is written as a column of values. It's as if R decided to put in newlines after each number 1.

Forgive me for being ignorant, but I always assumed that the point of having comma-separated-values was to express a sequence from left to right, of values, separated by commas. Sort of like I just did; in a sense mimicking the syntax of written word. Why does R cling so desperately to the column format when a csv so clearly should be a row?

All linguistic philosophy aside, I have tried to use the transpose function. I've dug through the documentation. Please help! Thanks.

Didi Bui
  • 623
  • 2
  • 7
  • 17

8 Answers8

5

write.csv is designed for matrices, and R treats a single vector as a matrix with a single column. Try making it into a matrix with one row and multiple columns and it should work as you expect.

write.csv(matrix(vector, nrow=1), file ="myfile.csv", row.names=FALSE)

Not sure what you tried with the transpose function, but that should work too.

write.csv(t(vector), file ="myfile.csv", row.names=FALSE)
Aaron left Stack Overflow
  • 36,704
  • 7
  • 77
  • 142
3

Here's what I did:

cat("myVar <- c(",file="myVars.r.txt", append=TRUE);

cat( myVar,       file="myVars.r.txt", append=TRUE, sep=", ");

cat(")\n",        file="myVars.r.txt", append=TRUE);

this generates a text file that can immediately be re-loaded into R another day using:

source("myVars.r.txt")
Kalamarico
  • 5,466
  • 22
  • 53
  • 70
2

Following up on what @Matt said, if you want a csv, try eol=",".

tumultous_rooster
  • 12,150
  • 32
  • 92
  • 149
2

I tried with this:

write.csv(rbind(vector), file ="myfile.csv", row.names=FALSE)

Output is getting written column wise, but, with column names.

This one seems to be better:

write.table(rbind(vector), file = "myfile.csv", row.names =FALSE, col.names = FALSE,sep = ",")

Now, the output is being printed as:

1   1   1   1   1

in the .csv file, without column names.

vamosrafa
  • 685
  • 5
  • 11
  • 35
1

write.table(vector, "myfile.csv", eol=" ", row.names=FALSE, col.names=FALSE)

You can simply change the eol to whatever you want. Here I've made it a space.

Matt
  • 437
  • 3
  • 10
1

You can use cat to append rows to a file. The following code would write a vector as a line to the file:

myVector <- c("a","b","c")
cat(myVector, file="myfile.csv", append = TRUE, sep = ",", eol = "\n")

This would produce a file that is comma-separated, but with trailing commas on each line, hence it is not a CSV-file.

If you want a real CSV-file, use the solution given by @vamosrafa. The code is as follows:

write.table(rbind(myVector), file = "myfile.csv", row.names =FALSE, col.names = FALSE,sep = ",", append = TRUE)

The output will be like this:

"a","b","c"

If the function is called multiple times, it will add lines to the file.

S.B.Wrede
  • 123
  • 1
  • 2
  • 10
0

One more:

write.table(as.list(vector), file ="myfile.csv", row.names=FALSE, col.names=FALSE, sep=",")
IRTFM
  • 258,963
  • 21
  • 364
  • 487
0

fwrite from data.table package is also another option:

library(data.table)

vector <- c(1,1,1,1,1)
fwrite(data.frame(t(vector)),file="myfile.csv",sep=",",row.names = FALSE)
Sam S.
  • 627
  • 1
  • 7
  • 23