2017-04-30 86 views
2

我有一個單詞列表,我想輸出到一個文本文件。例如:向垂直列中的文本文件寫入向量

words <- c("a", "and", "book", "cat", "car", "door", "donkey", "ogre", "princess", "rain") 
write(words, file = "test.out", ncolumns = 5, sep = "\t") 

這工作,但它給了我爲了話水平:一,和,書,貓,車內第一排,然後門,驢,食人魔,小公主,在第二雨行。我想讓訂單下降。 (顯然,實際列表比這個例子長得多)。

任何方式來做到這一點?

謝謝。

回答

0

解決方法如下:Write lines of text to a file in R。在此基礎上:

fileConn <- file("test.out")  
writeLines(c("a", "and", "book", "cat", "car", "door", "donkey", "ogre", "princess", "rain"), fileConn)  
close(fileConn) 
0

你可以試試這個:

nc <- 5 
nr <- length(words)/nc 

mwords <- matrix(words, nr, nc) 
write.table(mwords, "test.csv", row.names = FALSE, col.names=FALSE, sep = "\t") 

其寫入文件:

"a" "book" "car" "donkey" "princess" 
"and" "cat" "door" "ogre" "rain" 

它傳播在5列的數據,寫入的條目列 - 明智的。