简体   繁体   中英

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

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.

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)

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")

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说的话,如果你想要一个csv,试试eol=","

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.

多一个:

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

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM