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

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

问题描述

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

  sequence1 <-seq $ b sequence2< -seq(21:40)
...

因此我使用:

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

但问题是,在一列中:

  1 
2
3
... $ b b 21
22
...
40

以将数据添加到列中,以便最终显示为:

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

使用R?

解决方案

write.table 或矩阵到文件。如果你想要两个使用 write.table 将两列data.frame(或矩阵)写入文件,那么你需要在<$ c $中创建一个对象c> R

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

请参阅?write.table ,以清楚说明函数的功能。



由@ JoshuaUlrich的注释,这不是一个真正的 R 问题,你不能附加一个列到csv文件,由于它的存储在磁盘上的方式。 p>

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

How I can do that using 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)

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

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天全站免登陆