t for matrix || data.frame row-column exchange, 矩阵行列变换

0
0
0
1. 云栖社区>
2. 博客>
3. 正文

## t for matrix || data.frame row-column exchange, 矩阵行列变换

t可用于矩阵或数据框的行列变换.
``````> x
[,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
[3,]    3    6    9   12

> t(x)
[,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9
[4,]   10   11   12``````

``````> x <- 1:10
> y <- 101:110
> data.frame(x,y)
x   y
1   1 101
2   2 102
3   3 103
4   4 104
5   5 105
6   6 106
7   7 107
8   8 108
9   9 109
10 10 110
> t(data.frame(x,y))
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
x    1    2    3    4    5    6    7    8    9    10
y  101  102  103  104  105  106  107  108  109   110``````

[参考]
1. help(t)

``````t                     package:base                     R Documentation

Matrix Transpose

Description:

Given a matrix or ‘data.frame’ ‘x’, ‘t’ returns the transpose of
‘x’.

Usage:

t(x)

Arguments:

x: a matrix or data frame, typically.

Details:

This is a generic function for which methods can be written.  The
description here applies to the default and ‘"data.frame"’
methods.

A data frame is first coerced to a matrix: see ‘as.matrix’.  When
‘x’ is a vector, it is treated as a column, i.e., the result is a
1-row matrix.

Value:

A matrix, with ‘dim’ and ‘dimnames’ constructed appropriately from
those of ‘x’, and other attributes except names copied across.

Note:

The _conjugate_ transpose of a complex matrix A, denoted A^H or
A^*, is computed as ‘Conj(t(A))’.

References:

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) _The New S

‘aperm’ for permuting the dimensions of arrays.

Examples:

a <- matrix(1:30, 5, 6)
ta <- t(a) ##-- i.e.,  a[i, j] == ta[j, i] for all i,j :
for(j in seq(ncol(a)))
if(! all(a[, j] == ta[j, ])) stop("wrong transpose")``````

+ 关注