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
第n行变成第n列.
> 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
     Language_.  Wadsworth & Brooks/Cole.

See Also:

     ‘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")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值