📜  在 R 编程中获取矩阵或数据帧的转置 - t()函数

📅  最后修改于: 2022-05-13 01:54:27.727000             🧑  作者: Mango

在 R 编程中获取矩阵或数据帧的转置 - t()函数

R 语言中的t()函数用于计算矩阵或数据框的转置。

示例 1:

# R program to illustrate
# t function
   
# Getting R Biochemical Oxygen Demand Dataset
BOD 
   
# Calling t() function
t(BOD) 

输出:

Time demand
1    1    8.3
2    2   10.3
3    3   19.0
4    4   16.0
5    5   15.6
6    7   19.8

       [, 1] [, 2] [, 3] [, 4] [, 5] [, 6]
Time    1.0  2.0    3    4  5.0  7.0
demand  8.3 10.3   19   16 15.6 19.8

示例 2:

# R program to illustrate
# t function
   
# Initializing a matrix with 
# 3 row and 3 columns
x <- matrix(1:9, 3, 3)
x
   
# Calling t() function
t(x)

输出:

[, 1] [, 2] [, 3]
[1, ]    1    4    7
[2, ]    2    5    8
[3, ]    3    6    9

     [, 1] [, 2] [, 3]
[1, ]    1    2    3
[2, ]    4    5    6
[3, ]    7    8    9