Yehuda
Yehuda

Reputation: 1893

Data frame writes to a single column in write.csv/write.table R

I'm trying to write a csv file (I would use write.xlsx, but for some reason that was giving me Java memory errors.... no matter, I'll do this instead), but if I used the following data frame:

id <- c(1,2,3,4,5)
email <- c('[email protected]','[email protected]','[email protected]/','[email protected]','[email protected]/')
sample <- data.frame(id,email)
write.table(sample, 'Me\\Raw List.csv',
           row.names = TRUE, col.names = TRUE, append = FALSE)

I get the data all in a single-column CSV, along with a row identifier like this:

id "email"
1 1 "[email protected]"
2 2 "[email protected]"
3 3 "[email protected]/"
4 4 "[email protected]"
5 5 "[email protected]/"

My question is two-part: 1) How do I separate this data into columns; and 2) How do I remove the row identifiers so that I can just use my id?

Upvotes: 0

Views: 3446

Answers (1)

Ben
Ben

Reputation: 784

write.table()'s standard separator is " " (check the docs). Use write.csv() or write.csv2() along with the parameter row.names=False instead.

write.csv(sample,file = "my_dir/my_file.csv", row.names=F)

row.names=F makes that a unique row identifier (basically an id like you have it) will not be written.

You may use write.table() as well but you'll have to pass additional parameters:

write.table(sample, file = "test.csv", row.names=F, sep=",", dec=".")

Upvotes: 2

Related Questions