Sometimes it is troublesome to encounter different lines or columns with different lengths. Although you can fill the file in R with the longest rows (columns), it is inconvenient, so I think of using perl to implement one.
A text file of an indefinite length separated by commas (,) must be transposed by columns.
Copy codeThe Code is as follows: 1, 2, 3, 4, 5, 6,
7, 8, 9,
10, 11, 12, 13,
Convert:
1, 7, 10,
2, 8, 11,
3, 9, 12,
4, 13,
5 ,,,
6 ,,,
The following is the complete code for your reference.
Copy codeThe Code is 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,
After preparation and learning, implement it by yourself. It is best to make a template and use it as needed.