gpt4 book ai didi

r - 使用存储在行 R 中的 key 将数据帧从宽格式转换为长格式

转载 作者:行者123 更新时间:2023-12-01 10:19:19 24 4
gpt4 key购买 nike

我正在使用 tidyverse 但也欢迎使用 base 解决方案。

有没有一种方法可以在不转置的情况下收集数据框,而不是key作为列名,key被存储连续。例如,假设我有一个名为 df 的小标题。

df <- tibble(a = c(5,3,5,6,2,"G1"),
b = c(5,3,5,6,2,"G1"),
c = c(8,2,6,4,1,"G2"),
d = c(8,2,6,4,1,"G2"),
e = c(9,3,7,8,4,"G3"),
f = c(9,3,7,8,4,"G3"),
g = c(6,5,2,1,8,"G4"),
h = c(6,5,2,1,8,"G4"))
df
# A tibble: 6 x 8
a b c d e f g h
<chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 5 5 8 8 9 9 6 6
2 3 3 2 2 3 3 5 5
3 5 5 6 6 7 7 2 2
4 6 6 4 4 8 8 1 1
5 2 2 1 1 4 4 8 8
6 G1 G1 G2 G2 G3 G3 G4 G4

要分组或聚集的组位于底行。有没有办法让 df 只有三列,这样 c、e 和 g 列聚集到 a 列中,d、f 和 h 列聚集到 b 列和行中6 变成 c 列?结果看起来像:

tibble(a = c(5,3,5,6,2,8,2,6,4,1,9,3,7,8,4,6,5,2,1,8),
b = c(5,3,5,6,2,8,2,6,4,1,9,3,7,8,4,6,5,2,1,8),
c = c("G1","G1","G1","G1","G1","G2","G2","G2","G2","G2",
"G3","G3","G3","G3","G3","G4","G4","G4","G4","G4"))
# A tibble: 20 x 3
a b c
<dbl> <dbl> <chr>
1 5 5 G1
2 3 3 G1
3 5 5 G1
4 6 6 G1
5 2 2 G1
6 8 8 G2
7 2 2 G2
8 6 6 G2
9 4 4 G2
10 1 1 G2
11 9 9 G3
12 3 3 G3
13 7 7 G3
14 8 8 G3
15 4 4 G3
16 6 6 G4
17 5 5 G4
18 2 2 G4
19 1 1 G4
20 8 8 G4

我想避免转置,因为我需要保留行和列的顺序,直到所有内容都被正确标记为止。

最佳答案

这是一个想法。

library(tidyverse)

df2 <- df %>%
t() %>%
as.data.frame(stringsAsFactors = FALSE) %>%
split(f = .$V6) %>%
map_dfr(~.x %>%
select(-V6) %>%
t() %>%
as.data.frame(stringsAsFactors = FALSE) %>%
setNames(c("a", "b")),
.id = "c") %>%
select(a, b, c) %>%
mutate_at(vars(-c), list(~as.numeric(.)))

df2
# a b c
# 1 5 5 G1
# 2 3 3 G1
# 3 5 5 G1
# 4 6 6 G1
# 5 2 2 G1
# 6 8 8 G2
# 7 2 2 G2
# 8 6 6 G2
# 9 4 4 G2
# 10 1 1 G2
# 11 9 9 G3
# 12 3 3 G3
# 13 7 7 G3
# 14 8 8 G3
# 15 4 4 G3
# 16 6 6 G4
# 17 5 5 G4
# 18 2 2 G4
# 19 1 1 G4
# 20 8 8 G4

关于r - 使用存储在行 R 中的 key 将数据帧从宽格式转换为长格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56048362/

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