Альтернативное решение с использованием purrr :: map и dplyr (что может быть, а может и не быть проще / более интуитивным, чем решение r2evans):
# Recreate your data:
test <- list(Name1 = matrix(data = c(0,2,0,1,0,0,1,0,1,0,0,0,1,0,1,0,0,0),
row = 3, ncol = 6,
dimnames = list(c("Spec1", "Spec2", "Spec3"),
c("c1", "c2", "c3", "c4", "c5", "c6"))),
Name2 = matrix(data = c(0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,1),
nrow = 3, ncol = 6,
dimnames = list(c("Spec1", "Spec4", "Spec5"),
c("c1", "c2", "c3", "c4", "c5", "c6"))))
df <- map_dfr(1:length(test), ~test[[.x]] %>%
as.data.frame() %>%
mutate(items = names(test[.x]),
specs = row.names(test[[.x]]),
combined_names = paste0(items, specs)) %>%
select(9, 1:6))
df
combined_names c1 c2 c3 c4 c5 c6
1 Name1Spec1 0 1 1 0 1 0
2 Name1Spec2 2 0 0 0 0 0
3 Name1Spec3 0 0 1 0 1 0
4 Name2Spec1 0 0 0 1 0 0
5 Name2Spec4 0 1 0 0 0 0
6 Name2Spec5 0 0 0 0 0 1
Это может быть немного проще для анализа, если мы вытащим преобразование как его собственную функцию:
df_extractor <- function(x) {
test[[x]] %>% as.data.frame() %>% # Take the data from each matrix and convert it into a data frame
mutate(items = names(test[x]), # This extracts the name of each list
specs = row.names(test[[x]]), # This extracts the original row names
combined_names = paste0(items, specs)) %>% # Concatenate them together in your style above
select(9, 1:6) # Select and reorder columns.
}
df <- map_dfr(1:length(test), ~df_extractor(.x)) # use map_dfr to bind the resulting data frames together.