1

Is there any simple way, when using sink() in R to write to a csv file, to write a row where the first 2 cells are blank, and the third cell has a specific string? It is not all that simple, and I am struggling with it. I've tried the following:

# this approach writes all 5 numbers in one (the first) line with 2 spaces between each number
sink('myfile.csv')
for(i in 1:5) {
  cat(c(' ', ' ', i))
}
sink()


# this approach writes each number on its own line, but in the 1st column with 2 spaces the number
sink('myfile.csv')
for(i in 1:5) {
  cat(c(' ', ' ', i), '\n')
}
sink()

# this approach throws an error
sink('myfile.csv')
for(i in 1:5) {
  a = data.frame(` ` = '', `  ` = '', `Requested Access?` = '')
  cat(a, '\n')
}
sink()

To verify, the desired output in the example I have above would be a CSV file with the numbers 1,2,3,4,5 each in their own row, each in column C. Any help with this is appreciated!

Canovice
  • 9,012
  • 22
  • 93
  • 211

1 Answers1

2

You need to add commas in the spaces:

sink('myfile.csv')
for(i in 1:5) {
  cat(c(' ,', ' ,', i), '\n')
}
sink()

Alternatively, using write.table to avoid the for loop:

write.table(data.frame(` `=" ", ` `= "", ` ` = 1:5),
          row.names=FALSE,
          col.names = FALSE, 
          file = "myfile.csv",
          sep=",")
Kelli-Jean
  • 1,417
  • 11
  • 17