gpt4 book ai didi

r - 确定子组索引

转载 作者:行者123 更新时间:2023-12-04 15:57:14 25 4
gpt4 key购买 nike

我有一个包含组和子组的大型数据框。我想确定每个组中子组的索引,如以下数据框的OUTPUT列所示:

df <- data.frame(
Group = factor(c("A","A","A","A","A","B","B","B","B")),
Subgroup = factor(c("a","a","b","b","b","a","a","b","b")),
OUTPUT = c(1,1,2,2,2,1,1,2,2)
)


我尝试了几种可能性,但都没有成功。我想使用 dplyr,但是我不确定该怎么做。以下代码返回意外结果。

require(dplyr)

df <- df %>%
group_by(Group) %>%
mutate(
OUTPUT_2 = dplyr::id(Subgroup)
)

#df
# Group Subgroup OUTPUT_2
# (fctr) (fctr) (int)
#1 A a 8
#2 A a 8
#3 A b 8
#4 A b 8
#5 A b 8
#6 B a 4
#7 B a 4
#8 B b 4
#9 B b 4


我感觉我已经接近了,但是还没到那儿。有人可以帮忙吗?

最佳答案

我们可以在factor中使用dplyr路由

library(dplyr)
df %>%
group_by(Group) %>%
mutate(OUTPUT = as.numeric(factor(Subgroup, levels= unique(Subgroup))))
# Group Subgroup OUTPUT
# <fctr> <fctr> <dbl>
#1 A a 1
#2 A a 1
#3 A b 2
#4 A b 2
#5 A b 2
#6 B a 1
#7 B a 1
#8 B b 2
#9 B b 2


或按“组”分组后,另一个选项是带有“子组”的 match元素的 unique

df %>%
group_by(Group) %>%
mutate(OUTPUT = match(Subgroup, unique(Subgroup)) )
# Group Subgroup OUTPUT
# <fctr> <fctr> <int>
#1 A a 1
#2 A a 1
#3 A b 2
#4 A b 2
#5 A b 2
#6 B a 1
#7 B a 1
#8 B b 2
#9 B b 2

关于r - 确定子组索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37262615/

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