Reputation: 1147
I have a mostly blank data frame that can be read using the input below:
structure(list(var1 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), var2 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), var3 = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA), var4 = c(NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA), var5 = c(NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA), var6 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA),
var7 = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), var8 = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA), var9 = c(NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA), var10 = c(NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA)), .Names = c("var1", "var2", "var3",
"var4", "var5", "var6", "var7", "var8", "var9", "var10"), row.names = c(NA,
10L), class = "data.frame")
I would like to write this to a TXT file where it looks like everything is formatted properly and can be easily opened in Excel (I know about write.xlsx
and while it solves this problem I want the file to be in txt
format).
write.table(df3, file="test.txt", quote = FALSE, na = " \t", row.names = FALSE)
I tried this but the values do not align properly: for example, the values that should be under var7
show up on var6
intead. Also, while Excel manages to read the columns as being separate, the header row which contains the variable names gets read as one long vector. What gives?
Upvotes: 0
Views: 698
Reputation: 61154
Use write.table(df3, file="test.txt", quote = FALSE, sep = "\t", na="", row.names = FALSE)
Note that sep = "\t"
is used instead of na = " \t"
and set na=""
.
Upvotes: 1
Reputation: 1147
If I write:
write.table(df3, file="test.txt", quote = FALSE, sep="\t", row.names = FALSE, na="")
Then it works. I followed Jilber's advice about using sep="\t"
and also doing the this for the na
option, as otherwise the NAs would show up in the file.
Upvotes: 0