简体   繁体   English

使用write.table将结果追加到R中的现有文件

[英]Appending result to existing file in R using write.table

I have done a text mining on a file in R , then appended it to the existing file as new column. 我已经在R中的文件上进行了文本挖掘,然后将其作为新列添加到现有文件中。 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. 然后将原始结果也转换为数据框grp1作为Origin,并将grp2转换为Result,现在需要print语句帮助,以使用for循环在输出文件的单个行中打印两个文件的相同行。

i have used this code while print statement throwing some error, 我在print语句抛出一些错误时使用了此代码,

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

have got the desired output by this code: 通过以下代码获得了所需的输出:

df <- cbind(origin, result)


# Move to new file
     write.table(df, file="new_OT5.csv", sep=",", append=TRUE , row.names=FALSE, col.names=FALSE)
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. 现在,如何使用write.table呈现它已超出了我每天的常规使用范围。 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. 需要注意的一件事是,您可能会像上例一样以\\n结尾,在合并与同一行中的任何数据之前,需要对此进行条纹处理。 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 除此之外,它还很简单

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

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