将列转换为行

时间:2011-04-29 08:37:25

标签: r data-manipulation

例如,我有这个数据

Rows <- c(1,1,1,1,1,1,1,1,1,
    2,2,2,2,2,2,2,2,2,
    3,3,3,3,3,3,3,3,3,
    4,4,4,4,4,4,4,4,4,
    5,5,5,5,5,5,5,5,5,
    6,6,6,6,6,6,6,6,6,
    7,7,7,7,7,7,7,7,7,
    8,8,8,8,8,8,8,8,8,
    9,9,9,9,9,9,9,9,9)
Columns <- c(1,2,3,4,5,6,7,8,9,
    1,2,3,4,5,6,7,8,9,
    1,2,3,4,5,6,7,8,9,
    1,2,3,4,5,6,7,8,9,
    1,2,3,4,5,6,7,8,9,
    1,2,3,4,5,6,7,8,9,
    1,2,3,4,5,6,7,8,9,
    1,2,3,4,5,6,7,8,9,
    1,2,3,4,5,6,7,8,9)
Data <- c(1:81)

Dataset <- cbind(Rows,Columns)
Dataset <- cbind(Dataset,Data)

但这些是3列。看起来像这样:

       Rows Columns Data
[1,]    1       1    1
[2,]    1       2    2
[3,]    1       3    3
[4,]    1       4    4
[5,]    1       5    5
[6,]    1       6    6

我需要的是一个如下所示的表:,Row-column是RowNrs,Column-column是ColumnNrs,数据就是它尊重的地方

       [,1][,2][,3][,4][,5][,6][,7][,8][,9]
[1,]    1   2   3   4   5   6   7   8   9
[2,]    10  11  12  13  14  15  16  17  18
[3,]    19  20  21  22  23  24  25  26  27
[4,]    28  29  30  31  32  33  34  35  36
[5,]    37  38  39  40  41  42  43  44  45
[6,]    46  47  48  49  50  51  52  53  54
[7,]    55  56  57  58  59  60  61  62  63
[8,]    64  65  66  67  68  69  70  71  72
[9,]    73  74  75  76  77  78  79  80  81

有没有办法将“数据集”操作到此表中?

4 个答案:

答案 0 :(得分:1)

也许是

matrix(Data, nrow = 9, ncol = 9, byrow = T)

答案 1 :(得分:1)

只是出现了问题:

m <- matrix(1:81, ncol = 9, nrow = 9, byrow = TRUE)
rownames(m) <- colnames(m) <- 1:9

> m
   1  2  3  4  5  6  7  8  9
1  1  2  3  4  5  6  7  8  9
2 10 11 12 13 14 15 16 17 18
3 19 20 21 22 23 24 25 26 27
4 28 29 30 31 32 33 34 35 36
5 37 38 39 40 41 42 43 44 45
6 46 47 48 49 50 51 52 53 54
7 55 56 57 58 59 60 61 62 63
8 64 65 66 67 68 69 70 71 72
9 73 74 75 76 77 78 79 80 81

或者如果Data是特别的话:

> Dataset <- cbind(Rows, Columns, 1:81)
> matrix(Dataset[,3], ncol = max(Dataset[, 2]), nrow = max(Dataset[, 1]), 
+        byrow = TRUE)
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
 [1,]    1    2    3    4    5    6    7    8    9
 [2,]   10   11   12   13   14   15   16   17   18
 [3,]   19   20   21   22   23   24   25   26   27
 [4,]   28   29   30   31   32   33   34   35   36
 [5,]   37   38   39   40   41   42   43   44   45
 [6,]   46   47   48   49   50   51   52   53   54
 [7,]   55   56   57   58   59   60   61   62   63
 [8,]   64   65   66   67   68   69   70   71   72
 [9,]   73   74   75   76   77   78   79   80   81

答案 2 :(得分:1)

对于这种特殊情况,这应该有用。

Dataset <- as.data.frame(cbind(Dataset,Data))

dts <- split(Dataset, Dataset$Rows)
dts <- lapply(dts, function(x) {
            t(x$Data)
        })
> do.call("rbind", dts)
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
 [1,]    1    2    3    4    5    6    7    8    9
 [2,]   10   11   12   13   14   15   16   17   18
 [3,]   19   20   21   22   23   24   25   26   27
 [4,]   28   29   30   31   32   33   34   35   36
 [5,]   37   38   39   40   41   42   43   44   45
 [6,]   46   47   48   49   50   51   52   53   54
 [7,]   55   56   57   58   59   60   61   62   63
 [8,]   64   65   66   67   68   69   70   71   72
 [9,]   73   74   75   76   77   78   79   80   81

答案 3 :(得分:1)

我不确定这是你想要的,但从你的例子来看,它似乎可以解决问题。您实际上不需要行号和列号,只需要数据:

matrix(Data, nrow=9, ncol=9, byrow=T)