如何使用 R 在 csv 文件中按列添加数据? [英] How to add data by columns in csv file using R?

查看:107
本文介绍了如何使用 R 在 csv 文件中按列添加数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这篇关于如何使用 R 在 csv 文件中按列添加数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆