如何使用 R 在 csv 文件中按列添加数据?如何使用、文件、数据、csv

2023-09-07 02:38:34 作者:︶ㄣ满天花瓣渲染离别ㄣ︶

我有包含在向量中的信息,例如:

I have information that is contained in vectors, for example:

sequence1<-seq(1:20)
sequence2<-seq(21:40)
...

我想将该数据附加到文件中,所以我正在使用:

I want to append that data to a file, so I am using:

write.table(sequence1,file="test.csv",sep=",",append=TRUE,row.names=FALSE,col.names=FALSE)
write.table(sequence2,file="test.csv",sep=",",append=TRUE,row.names=FALSE,col.names=FALSE)

但问题在于,这是全部添加在一列中,例如:

But the issue is that this is added all in one column like:

1
2
3
...
21
22
...
40

我想在列中添加该数据,以便它最终如下:

I want to add that data in columns so that it ends up like:

1         21
2         22
3         23
...       ...
20        40

我如何使用 R 来做到这一点?

How I can do that using R?

推荐答案

write.table 将 data.frame 或矩阵写入文件.如果你想要两个使用 write.table 将两列 data.frame(或矩阵)写入文件,那么你需要在 R

write.table writes a data.frame or matrix to a file. If you want two write a two-column data.frame (or matrix) to a file using write.table, then you need to create such an object in R

x <- data.frame(sequence1, sequence2)
write.table(x, file = 'test.csv', row.names=FALSE,col.names=FALSE)

请参阅 ?write.table 以获得对该函数作用的非常清晰的描述.

See ?write.table for a very clear description of what the function does.

正如@JoshuaUlrich 的评论所述,这不是真正的 R 问题,由于它在磁盘上的存储方式,您不能将列附加到 csv 文件.

As stated by @JoshuaUlrich's comment, this is not really an R issue, you can't append a column to a csv file due to the way it is stored on disk.