Skip to contents

stm_transpose() transposes a data frame, matrix, or tibble, converting it into the appropriate format after transposition.

Usage

stm_transpose(dat)

stm_t(dat)

Arguments

dat

A matrix, data frame, or tibble to be transposed.

Value

The transposed dat, either as a matrix, data frame, or tibble.

Details

The function first checks the class of dat and transposes it accordingly. If it's a data frame, it converts it into a standard data frame after transposition. If it's a tibble, it also cleans up the column names using janitor::clean_names(). If the data is a matrix or vector, it is directly transposed.

Examples

# Matrix example
A <- matrix(11:22, nrow = 3, byrow = TRUE)
print(A)
#>      [,1] [,2] [,3] [,4]
#> [1,]   11   12   13   14
#> [2,]   15   16   17   18
#> [3,]   19   20   21   22
stm_transpose(A)
#>      [,1] [,2] [,3]
#> [1,]   11   15   19
#> [2,]   12   16   20
#> [3,]   13   17   21
#> [4,]   14   18   22

# Data frame example
df <- data.frame(
  A = c(1, 2, 3),
  B = c(4, 5, 6)
)
print(df)
#>   A B
#> 1 1 4
#> 2 2 5
#> 3 3 6
stm_transpose(df)
#>   V1 V2 V3
#> A  1  2  3
#> B  4  5  6

# Tibble example
library(tibble)
tbl <- tibble(
  A = c(1, 2, 3),
  B = c(4, 5, 6)
)
print(tbl)
#> # A tibble: 3 × 2
#>       A     B
#>   <dbl> <dbl>
#> 1     1     4
#> 2     2     5
#> 3     3     6
stm_transpose(tbl)
#> # A tibble: 2 × 3
#>       x   x_2   x_3
#>   <dbl> <dbl> <dbl>
#> 1     1     2     3
#> 2     4     5     6