在垂直列中向文本文件写入一个向量

时间:2021-11-29 03:53:33

I have a list of words, which I want to output to a text file. For example:

我有一个单词列表,我想将它输出到一个文本文件中。例如:

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

This works, but it gives me the words in order horizontally: a, and, book, cat, car in the first row, then door, donkey, ogre, princess, rain in the second row. I want the order to go down columns. (Obviously, the actual list is much longer than this example).

这是可行的,但它给了我水平排列的单词:a, and, book, cat, car in the first row, then door, donkey, ogre, princess, rain in the second row。我想要向下列的顺序。(显然,实际的列表要比这个示例长得多)。

Any way to do that?

有什么办法吗?

Thanks.

谢谢。

2 个解决方案

#1


2  

You can try this:

你可以试试这个:

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

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

which writes to the file as:

将文件写入:

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

It spreads the data in 5 columns, writing the entries column-wise.

它将数据分散到5列中,以列方式写入条目。

#2


0  

Solution is here: Write lines of text to a file in R. Based on it:

解决方案如下:将一行文本写在r中的文件中,并以此为基础:

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

#1


2  

You can try this:

你可以试试这个:

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

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

which writes to the file as:

将文件写入:

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

It spreads the data in 5 columns, writing the entries column-wise.

它将数据分散到5列中,以列方式写入条目。

#2


0  

Solution is here: Write lines of text to a file in R. Based on it:

解决方案如下:将一行文本写在r中的文件中,并以此为基础:

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