Is an alternative to t()
to transpose a data frame. The first
column of df
will become column names in the transposed data.
Examples
# \donttest{
library(metan)
df <-
data.frame(
GEN = c("G1", "G2", "G3","G4"),
E1 = rnorm(4, 100, 20),
E2 = rnorm(4, 10, 2),
E3 = rnorm(4, 50, 5),
E4 = rnorm(4, 1000, 150)
)
df
#> GEN E1 E2 E3 E4
#> 1 G1 131.66380 10.293072 49.31684 1095.1549
#> 2 G2 79.14819 8.424353 51.12045 925.1208
#> 3 G3 99.83225 9.422005 55.58603 1168.9409
#> 4 G4 73.31824 11.333679 42.80753 984.9723
t(df)
#> [,1] [,2] [,3] [,4]
#> GEN "G1" "G2" "G3" "G4"
#> E1 "131.66380" " 79.14819" " 99.83225" " 73.31824"
#> E2 "10.293072" " 8.424353" " 9.422005" "11.333679"
#> E3 "49.31684" "51.12045" "55.58603" "42.80753"
#> E4 "1095.1549" " 925.1208" "1168.9409" " 984.9723"
transpose_df(df)
#> # A tibble: 4 × 5
#> name G1 G2 G3 G4
#> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 E1 132. 79.1 99.8 73.3
#> 2 E2 10.3 8.42 9.42 11.3
#> 3 E3 49.3 51.1 55.6 42.8
#> 4 E4 1095. 925. 1169. 985.
# }