How to write a csv file in R, where my input is written to the file as a string?

This is a very simple problem, and I am 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 csv as a simple line:

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

When I open the file I just wrote, csv is written as a column of values. It is as if R decided to set new lines after each number 1.

Forgive me for being ignorant, but I always assumed that a point with comma-separated values โ€‹โ€‹should express a sequence from left to right, comma-separated values. It looks like I just did; in a way mimicking the syntax of a written word. Why is R so desperately clinging to a column format when csv should be so clearly next?

I tried to use the transpose function throughout linguistic philosophy. I broke through the documentation. Please help! Thank you

+7
source share
8 answers

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

+1
source

write.csv is for matrices, and R processes a single vector as a matrix with one column. Try to make it into a matrix with one row and several columns, and it should work as you expect.

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

Not sure if you tried with the transpose function, but this should work too.

 write.csv(t(vector), file ="myfile.csv", row.names=FALSE) 
+5
source

Here is 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 be immediately reloaded into R another day using:

 source("myVars.r.txt") 
+2
source

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

You can simply change the eol as you want. Here I made this space.

+1
source

I tried with this:

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

The result is a column with a header, but with column names.

It seems to be better:

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

Now the output is as follows:

 1 1 1 1 1 

in a CSV file without column names.

+1
source

Another:

 write.table(as.list(vector), file ="myfile.csv", row.names=FALSE, col.names=FALSE, sep=",") 
0
source

You can use cat to add lines to the file. The following code will write the vector as a string to a file:

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

This will create a file separated by commas, but with commas in each line, therefore it is not a CSV file.

If you need 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 a function is called multiple times, it adds lines to the file.

0
source

fwrite from the 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) 
0
source

Source: https://habr.com/ru/post/959107/


All Articles