使用write.table将结果附加到R中的现有文件 [英] Appending result to existing file in R using write.table

查看:258
本文介绍了使用write.table将结果附加到R中的现有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经对R中的文件进行了文本挖掘,然后将其作为新列添加到现有文件中。完成后,该列将添加到实际内容之后。我该如何纠正?



结果如下所示:

 客户名称
T-MAX INDUSTRIAL LTD
T-MAX INDUSTRIAL LTD。
ADCHEM
ADCHEM(澳大利亚)
ADCHEM(澳大利亚)PTY LTD
AUROBINDO PHARMA(澳大利亚)
AUROBINDO PHARMA(澳大利亚)PTY
Aurobindo Pharma(澳大利亚) )Pty
AUROBINDO PHARMA(AUSTRALIA)PTY LTD
AUROBINDO PHARMA(澳大利亚)PTY LTD
V1
最大工业
最大工业
adchem
adchem
adchem
aurobindo pharma
aurobindo pharma
aurobindo pharma
aurobindo pharma
aurobindo pharma

但是我的输出结果如下所示:

 客户名称V1 
T-MAX INDUSTRIAL LTD最高工业
T-MAX INDUSTRIAL LTD。 t澳大利亚工业
ADCHEM adchem
ADCHEM(澳大利亚)adchem
ADCHEM(澳大利亚)PTY LTD adchem
AUROBINDO PHARMA(澳大利亚)aurobindo pharma
AUROBINDO PHARMA(澳大利亚)PTY aurobindo (澳大利亚)aurobindo pharma(澳大利亚)aurobindo pharma(澳大利亚)aurobindo pharma(澳大利亚)aurobindo pharma
AUROBINDO PHARMA(澳大利亚)PTY LTD aurobindo pharma
b / b
AUROBINDO PHARMA(澳大利亚)PTY aurobindo pharma
AUROBINDO PHARMA



我使用的代码是:

  result< ; data.frame(text = unlist(sapply(b,```)),stringsAsFactors = FALSE)
write.table(result,file =Counter_Party_Testing.csv,sep =。,eol = \\\
,append = TRUE,row.names = FALSE,col.names = FALSE)

然后把原来的结果转换为数据帧grp1作为Origin,而grp2作为Result,现在需要使用print语句来帮助打印相同的行e



我使用这段代码,而print语句抛出一些错误,

print $ {
print(grp1 [n] +','+ grp2 [
$ p $ n] +)
}


解决方案


打开('file-合并:
merge_data = origin.readlines()

与open('output.csv','w'作为输出:
for我在范围(len(original_data)):
output.write(original_data [i] .strip()+','+ merge_data [i] .strip()+'\\\
')

这解决了您的数据未正确合并的问题。现在你如何使用 write.table 来呈现它,而不是我每天使用的正常范围。但就原始数据而言,这解决了这个问题。你可以把最终结果放在一个字符串中,而不是把它写到磁盘上。



有一点需要注意的是,一个尾随的 \\\
就像上面的例子一样,在合并同一行的任何数据之前,需要对它进行条带化处理。如果合并数据与原始数据长度不一样或长度更长,索引也可能超出范围。但是,这是错误处理,我会把它留给你,我刚刚提供了一个解决方案的概念在你的问题。除此之外,它是非常简单的编码


I have done a text mining on a file in R , then appended it to the existing file as new column. When it is done, the column is added after the actual content. How do I correct this?

The result looks like this:

Customer Names
T-MAX INDUSTRIAL LTD
T-MAX INDUSTRIAL LTD.
ADCHEM
ADCHEM (AUSTRALIA)
ADCHEM (AUSTRALIA) PTY LTD
AUROBINDO PHARMA (AUSTRALIA)
AUROBINDO PHARMA (AUSTRALIA) PTY
Aurobindo Pharma (Australia) Pty
AUROBINDO PHARMA(AUSTRALIA) PTY LTD
AUROBINDO PHARMA(AUSTRALIA)PTY LTD
V1
tmax industrial 
tmax industrial 
adchem
adchem  
adchem   
aurobindo pharma  
aurobindo pharma  
aurobindo pharma  
aurobindo pharma   
aurobindo pharma   

But my output must look like this :

Customer Names                           V1 
T-MAX INDUSTRIAL LTD                    tmax industrial     
T-MAX INDUSTRIAL LTD.                   tmax industrial     
ADCHEM                                  adchem  
ADCHEM (AUSTRALIA)                      adchem      
ADCHEM (AUSTRALIA) PTY LTD              adchem      
AUROBINDO PHARMA (AUSTRALIA)            aurobindo pharma    
AUROBINDO PHARMA (AUSTRALIA) PTY        aurobindo pharma    
AUROBINDO PHARMA (AUSTRALIA) PTY        aurobindo pharma    
AUROBINDO PHARMA(AUSTRALIA) PTY LTD     aurobindo pharma    
AUROBINDO PHARMA(AUSTRALIA)PTY LTD      aurobindo pharma    

The code I used is:

result <- data.frame(text=unlist(sapply(b, `[`)), stringsAsFactors=FALSE)
write.table(result, file="Counter_Party_Testing.csv", sep=".", eol="\n", append=TRUE, row.names=FALSE, col.names=FALSE)

and then the result as well Original is converted to dataframe grp1 as Origin , and grp2 as Result , now need help with print statement to print same rows each of both files at a single row in output file using for loop.

i have used this code while print statement throwing some error,

for (n in seq_len(nrow(grp1))) 
{
    print(grp1[n]+','+grp2[n]+) 
}

解决方案

with open('file-one.txt', 'r') as origin:
    original_data = origin.readlines()

with open('file-two.txt', 'r' as merge:
    merge_data = origin.readlines()

with open('output.csv', 'w' as output:
    for i in range(len(original_data)):
        output.write(original_data[i].strip() + ', ' + merge_data[i].strip() + '\n')

This solved the problem where your data isn't merged properly. Now how you use write.table to present it is without my scope of normal every day use. But in terms of raw data, this solves it. You could put the end-result in a string instead of writing it to disk if you'd like.

One thing to note is that you'll probably end up with a trailing \n like the above example, this needs to be striped before merging with any data on the same row. Also the index might get out of range if the merge-data isn't of the same or greater length as the origin data. But that's error handling and i'll leave it to you, I've just provided a concept of a solution on your problem. Other than that it's pretty straight forward coding

这篇关于使用write.table将结果附加到R中的现有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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