gpt4 book ai didi

r - 按组分割数据帧并水平堆叠

转载 作者:行者123 更新时间:2023-12-02 19:25:39 25 4
gpt4 key购买 nike

我有一个数据框

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和likes水平绑定(bind)它们,但它不起作用。

谢谢!

最佳答案

我们可以在创建序列列后使用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>

关于r - 按组分割数据帧并水平堆叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62437023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com