摆脱列和转置表

时间:2014-05-13 10:28:29

标签: r perl bash

我在文本文件中有一个表,它是以空格分隔的。因为它非常大,我真的不能使用R.是否有一种简单的方法来摆脱第一列,然后转置表,然后添加到indexcolums(仅从1到nrow表)作为第1列和第2列?

此刻的表格似乎是

1 0 0 0 0 0 1 0
2 0 0 1 0 0 0 0
3 0 0 0 0 0 0 0
4 1 1 1 2 3 0 0

R,perl或bash(awk)是否有快速方法可以做到这一点?

2 个答案:

答案 0 :(得分:0)

如果您想使用Perl,可以试用PDL模块。这是您的代码的基本示例:

use strict;
use warnings;
use PDL;

my @array = (
  [ 1, 0, 0, 0, 0, 0, 1, 0, ],
  [ 2, 0, 0, 1, 0, 0, 0, 0, ],
  [ 3, 0, 0, 0, 0, 0, 0, 0, ],
  [ 4, 1, 1, 1, 2, 3, 0, 0, ],
);

# unshift first element of each line
unshift @{$_} foreach @array;

my $m = PDL->pdl( @array );
print $m->transpose;

输出:

[
 [1 2 3 4]
 [0 0 0 1]
 [0 0 0 1]
 [0 1 0 1]
 [0 0 0 2]
 [0 0 0 3]
 [1 0 0 0]
 [0 0 0 0]
]

更多信息:here

答案 1 :(得分:0)

由于你只是在没有变量的数值中表达你的例子,我假设你宁愿使用矩阵。使用基本函数,您可以执行以下操作(删除列@Vincent:

table <- matrix(c(1,0,0,0,0,0,1,0,
 +                2,0,0,1,0,0,0,0,
 +                3,0,0,0,0,0,0,0,
 +                4,1,1,1,2,3,0,0), 
 +                nrow = 4, ncol = 7, byrow=TRUE) #Recreates your example above

table <- table[,-1] #Removes the first column by index number (as indicated by @Vincent in your comment above)

table <- t(table) #Transposes your matrix

table 
     [,1] [,2] [,3] [,4]
[1,]    0    0    0    1
[2,]    0    0    0    1
[3,]    0    1    0    1
[4,]    0    0    0    2
[5,]    0    0    0    3
[6,]    1    0    0    0
[7,]    0    0    0    0 #Output