0

在合并/连接/组合/绑定等方面需要帮助

  1. 我有几个 ascii 文件,每个文件都定义一个变量,我已将其转换为单列数组

我有许多变量的列数据,所以我需要像执行列绑定一样R将其设为一个文件。

我可以这样做,R但文件太多。能够用一个代码完成这将有助于节省大量时间。

使用以下代码,perl 新手,需要帮助。

@filenames = ("file1.txt","file2.txt");
open F2, ">file_combined.txt" or die;
for($j = 0; $j< scalar @filenames;$j++){
    open F1, $filenames[$j] or die;
    for($i=1;$i<=6;$i++){$line=<F1>;}
    while($line=<F1>){
        chomp $line;
        @spl = split '\s+', $line;
        for($i=0;$i<scalar @spl;$i++){
            print F2 "$spl[$i]\n";
            paste "file_bio1.txt","file_bio2.txt"> file_combined.txt;
        }
    }
    close F1;
}

此处的输入文件是栅格的 Ascii 文本文件。它们看起来像这样

32 12 34 21 32 21 22 23 
12 21 32 43 21 32 21 12 

上面提到的没有粘贴语法的代码将这些文件转换为单个列

32 
12 
34  
21 
32 
21 
22 
23
12 
21
32 
43 
21  
32 
21 
12 


The output should look like this
12  21  32
32  23  23
32  21  32
12  34  12
43  32  32
32  23  23
32  34  21
21  32  23

每列代表一个不同的 ascii 文件。我需要大约 15 个这样的 ascii 文件到一个数据帧中。我可以在 R 中做同样的事情,但它会消耗大量时间,因为文件和感兴趣区域的数量太多并且文件也有点大。

4

1 回答 1

1

让我们逐步了解您所拥有的...

# files you want to open for reading..
@filenames = ("file1.txt","file2.txt");

# I would use the 3 arg lexical scoped open
# I think you want to open this for 'append' as well
# open($fh, ">>", "file_combined.txt") or die "cannot open";
open F2, ">file_combined.txt" or die;

# @filenames is best thought as a 'list'
# for my $file (@filenames) {
for($j = 0; $j< scalar @filenames;$j++){
    # see above example of 'open'
    # - $filenames[$j] + $file
    open F1, $filenames[$j] or die;

    # what are you trying to do here? You're overriding 
    # $line in the next 'while loop'
    for($i=1;$i<=6;$i++){$line=<F1>;}
    # while(<$fh1>) {
    while($line=<F1>){
        chomp $line;
        # @spl is short for split? 
        # give '@spl' list a meaningful name
        @spl = split '\s+', $line;
        # again, @spl is a list...
        # for my $word (@spl) {
        for($i=0;$i<scalar @spl;$i++){
            # this whole block is a bit confusing. 
            # 'F2' is 'file_combined.txt'. Then you try and merge
            # ( and overwrite the file) with the paste afterwards...
            print F2 "$spl[$i]\n";
            # is this a 'system call'? 
            # Missing 'backticks' or 'system'
            paste "file_bio1.txt","file_bio2.txt"> file_combined.txt;
        }
    }
    # close $fh1
    close F1;
}
# I'm assuming there's a 'close F2' somewhere here..

看起来你正在尝试这样做:

@filenames = ("file1.txt","file2.txt");
$oufile = "combined_text.txt";
`paste $filenames[0] $filenames[1] > $outfile`;
于 2013-04-03T18:00:28.367 回答