简体   繁体   中英

write.table() append argument

When i run the following code the csv file has the 'DF' data frame followed by an 'x' on the next row then 'notes' on the row after.

Why is it outputting the 'x' and is there a way to stop it doing that? I want just the data frame followed by 'notes'.

DF <- data.frame(numbers=1:3, letters=c("a","b","c"))
write.table(DF, file="filename.csv", sep=",", row.names=FALSE)

write.table( "notes", file="filename.csv", append=TRUE, sep=",", 
row.names=FALSE)

output:

numbers letters
1       a
2       b
3       c
x
notes

The documentation of write.table says this:

x: the object to be written, preferably a matrix or data frame. If not, it is attempted to coerce x to a data frame.

Since you don't pass a data.frame, this happens internally:

x <- "notes"; x <- data.frame(x)

This creates the x as a column name:

print(x)
      x
1 notes

You can set col.names = FALSE to prevent writing it to the file.

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