Sometimes it is troublesome to encounter rows or columns that are inconsistent in length, although you can fill the file with the longest row (column) in R, but it is inconvenient, so remember to use Perl to achieve one.
The indefinite length text file separated by commas is now implemented as a row-and-column transpose, which is
Copy Code code as follows:
1,2,3,4,5,6,
7,8,9,
10,11,12,13,
Convert to:
1,7,10,
2,8,11,
3,9,12,
4,, 13,
5,,,
6,,,
The following is the complete code for your reference.
Copy Code code as follows:
#!/usr/bin/perl-w
My @matrix;
My $max _len = 0;
while (<DATA>) {
Chomp
s/,$//g;
My @fields = Split/,/, $_;
my $len = @fields;
$max _len = $max _len > $len? $max _len: $len;
Push @matrix, [@fields];
}
For my $col (0. $max _len-1) {
For my $line (@matrix) {
Print $line->[$col] | | ' ', ',';
}
print "\ n";
}
__data__
1,2,3,4,5,6,
7,8,9,
10,11,12,13,
Ready to learn, do it yourself, the best is to make a template, Tiaogan with.