简体   繁体   English

将txt文件另存为R中定界的csv文件

[英]Saving a txt file as a delimited csv file in R

I have the following code to read a file and save it as a csv file, I remove the first 7 lines in the text file and then the 3rd column as well, since I just require the first two columns. 我有以下代码读取文件并将其保存为csv文件,我删除了文本文件中的前7行,然后也删除了第3列,因为我只需要前两列。

 current_file <- paste("Experiment 1 ",i,".cor",sep="")
 curfile <- list.files(pattern = current_file)
 curfile_data <- read.table(curfile, header=F,skip=7,sep=",")
 curfile_data <- curfile_data[-grep('V3',colnames(curfile_data))]
 write.csv(curfile_data,curfile)
 new_file <- paste("Dev_C",i,".csv",sep="")
 new_file 
 file.copy(curfile, new_file)

The curfile thus hold two column variables V1 and V2 along with the observation number column in the beginning. 因此,该curfile文件在开头具有两个列变量V1和V2以及观察编号列。

Now when I use file.copy to copy the contents of the curfile into a .csv file and then open the new .csv file in Excel, all the data seems to be concatenated and appear in a single column, is there a way to show each of the individual columns separately? 现在,当我使用file.copy将curfile的内容复制到.csv文件中,然后在Excel中打开新的.csv文件时,所有数据似乎已连接并显示在单个列中,有没有一种方法可以显示每个单独的列分别? Thanks in advance for your suggestions. 预先感谢您的建议。


The data in the .txt file looks like this, .txt文件中的数据如下所示,

"","V1","V2","V3"
"1",-0.02868862,5.442283e-11,76.3
"2",-0.03359281,7.669754e-12,76.35
"3",-0.03801883,-1.497323e-10,76.4
"4",-0.04320051,-6.557672e-11,76.45
"5",-0.04801207,-2.557059e-10,76.5
"6",-0.05325544,-9.986231e-11,76.55

You need to use Text to columns feature in Excel, selecting comma as a delimiter. 您需要在Excel中使用“ Text to columns功能,选择逗号作为分隔符。 The position of this point in menu depends on version of Excel you are using. 菜单中此点的位置取决于您使用的Excel版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM