Надеюсь, это поможет!
library(dplyr)
library(tidyr)
library(tibble)
df %>%
rownames_to_column("rowname") %>%
separate(rowname, c("rowname_prefix", "rowname_suffix"), "-") %>%
gather(col_name, value, -rowname_prefix, -rowname_suffix) %>%
mutate(col_name = paste(col_name, rowname_prefix, sep="-")) %>%
select(-rowname_prefix) %>%
spread(col_name, value) %>%
mutate(rowname_suffix = paste0("R", rowname_suffix)) %>%
column_to_rownames("rowname_suffix")
Вывод:
C1-1 C1-2 C1-3 C2-1 C2-2 C2-3 C3-1 C3-2 C3-3 C4-1 C4-2 C4-3
R1 100 109 150 36 26 14 136 166 164 76 87 24
R2 120 -33 100 -33 87 36 87 42 765 42 24 76
R3 150 100 150 14 36 14 164 136 164 24 76 94
R4 20 100 10 36 36 26 136 136 106 76 76 76
Пример данных:
df <- structure(list(C1 = c(100L, 120L, 150L, 20L, 109L, -33L, 100L,
100L, 150L, 100L, 150L, 10L), C2 = c(36L, -33L, 14L, 36L, 26L,
87L, 36L, 36L, 14L, 36L, 14L, 26L), C3 = c(136L, 87L, 164L, 136L,
166L, 42L, 136L, 136L, 164L, 765L, 164L, 106L), C4 = c(76L, 42L,
24L, 76L, 87L, 24L, 76L, 76L, 24L, 76L, 94L, 76L)), .Names = c("C1",
"C2", "C3", "C4"), class = "data.frame", row.names = c("1-1",
"1-2", "1-3", "1-4", "2-1", "2-2", "2-3", "2-4", "3-1", "3-2",
"3-3", "3-4"))
# C1 C2 C3 C4
#1-1 100 36 136 76
#1-2 120 -33 87 42
#1-3 150 14 164 24
#1-4 20 36 136 76
#2-1 109 26 166 87
#2-2 -33 87 42 24
#2-3 100 36 136 76
#2-4 100 36 136 76
#3-1 150 14 164 24
#3-2 100 36 765 76
#3-3 150 14 164 94
#3-4 10 26 106 76