разделить фрейм данных на группу и сложить по горизонтали - PullRequest
2 голосов
/ 17 июня 2020

У меня есть фрейм данных

structure(list(name = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 1L, 12L, 10L, 14L, 11L, 13L, 14L), .Label = c("a", "b", 
"c", "d", "e", "f", "g", "h", "i", "p", "q", "r", "s", "w"), class = "factor"), 
    total = c(1, 2, 3, 4, 5, 6, 7, 2, 3, 4, 1, 4, 9, 3, 4, 6), 
    freq = c(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.2, 0.3, 0.4, 
    0.1, 0.4, 0.9, 0.3, 0.4, 0.6), group = structure(c(1L, 1L, 
    1L, 4L, 4L, 4L, 2L, 2L, 2L, 5L, 5L, 5L, 3L, 3L, 6L, 6L), .Label = c("X_Group1", 
    "X_Group2", "X_Group3", "Y_Group1", "Y_Group2", "Y_Group3"
    ), class = "factor")), class = "data.frame", row.names = c(NA, 
-16L))

Можно ли преобразовать его, как показано ниже

result <- structure(list(X_Group1.name = structure(1:3, .Label = c("a", 
"b", "c"), class = "factor"), X_Group1.total = c(1, 2, 3), X_Group1.freq = c(0.1, 
0.2, 0.3), X_Group1.group = structure(c(1L, 1L, 1L), .Label = "X_Group1", class = "factor"), 
    X_Group2.name = structure(1:3, .Label = c("g", "h", "i"), class = "factor"), 
    X_Group2.total = c(7, 2, 3), X_Group2.freq = c(0.7, 0.2, 
    0.3), X_Group2.group = structure(c(1L, 1L, 1L), .Label = "X_Group2", class = "factor"), 
    X_Group3.name = structure(3:1, .Label = c("NA", "q", "w"), class = "factor"), 
    X_Group3.total = structure(c(2L, 1L, 3L), .Label = c("3", 
    "9", "NA"), class = "factor"), X_Group3.freq = structure(c(2L, 
    1L, 3L), .Label = c("0.3", "0.9", "NA"), class = "factor"), 
    X_Group3.group = structure(c(2L, 2L, 1L), .Label = c("NA", 
    "X_Group3"), class = "factor"), Y_Group1.name = structure(1:3, .Label = c("d", 
    "e", "f"), class = "factor"), Y_Group1.total = c(4, 5, 6), 
    Y_Group1.freq = c(0.4, 0.5, 0.6), Y_Group1.group = structure(c(1L, 
    1L, 1L), .Label = "Y_Group1", class = "factor"), Y_Group2.name = structure(c(1L, 
    3L, 2L), .Label = c("a", "p", "r"), class = "factor"), Y_Group2.total = c(4, 
    1, 4), Y_Group2.freq = c(0.4, 0.1, 0.4), Y_Group2.group = structure(c(1L, 
    1L, 1L), .Label = "Y_Group2", class = "factor"), Y_Group3.name = structure(c(2L, 
    3L, 1L), .Label = c("NA", "s", "w"), class = "factor"), Y_Group3.total = structure(1:3, .Label = c("4", 
    "6", "NA"), class = "factor"), Y_Group3.freq = structure(1:3, .Label = c("0.4", 
    "0.6", "NA"), class = "factor"), Y_Group3.group = structure(c(2L, 
    2L, 1L), .Label = c("NA", "Y_Group3"), class = "factor")), class = "data.frame", row.names = c(NA, 
-3L))

Я пробовал разделить, а затем привязать их по горизонтали с помощью bind_col и любит, но это не так работай.

Спасибо!

1 Ответ

5 голосов
/ 17 июня 2020

Мы можем использовать pivot_wider после создания столбца последовательности

library(dplyr)
library(data.table)
library(tidyr)
df1 %>%
  mutate(rn = rowid(group)) %>% 
  pivot_wider(names_from = group, values_from = c(name, total, freq))
# A tibble: 3 x 19
#     rn name_X_Group1 name_Y_Group1 name_X_Group2 name_Y_Group2 name_X_Group3 name_Y_Group3 total_X_Group1 total_Y_Group1 total_X_Group2
#  <int> <fct>         <fct>         <fct>         <fct>         <fct>         <fct>                  <dbl>          <dbl>          <dbl>
#1     1 a             d             g             a             w             s                          1              4              7
#2     2 b             e             h             r             q             w                          2              5              2
#3     3 c             f             i             p             <NA>          <NA>                       3              6              3
# … with 9 more variables: total_Y_Group2 <dbl>, total_X_Group3 <dbl>, total_Y_Group3 <dbl>, freq_X_Group1 <dbl>, freq_Y_Group1 <dbl>,
#   freq_X_Group2 <dbl>, freq_Y_Group2 <dbl>, freq_X_Group3 <dbl>, freq_Y_Group3 <dbl>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...