gpt4 book ai didi

r - 使用 dplyr tidyr 在汇总表中保留输入变量和因子水平的顺序

转载 作者:行者123 更新时间:2023-12-04 16:33:26 25 4
gpt4 key购买 nike

我爱多么容易dplyrtidyr已经创建了一个带有多个预测变量和结果变量的汇总表。让我难倒的一件事是在输出表中保留/定义预测变量的顺序及其因子水平的最后一步。

我想出了一个解决方案(如下),其中涉及使用 mutate手动制作一个因子变量,该变量将预测变量和预测变量值(例如“gender_female”)与所需输出顺序中的级别相结合。但是如果有很多变量,我的解决方案有点啰嗦,不知道有没有更好的方法?

library(dplyr)
library(tidyr)
levels_eth <- c("Maori", "Pacific", "Asian", "Other", "European", "Unknown")
levels_gnd <- c("Female", "Male", "Unknown")

set.seed(1234)

dat <- data.frame(
gender = factor(sample(levels_gnd, 100, replace = TRUE), levels = levels_gnd),
ethnicity = factor(sample(levels_eth, 100, replace = TRUE), levels = levels_eth),
outcome1 = sample(c(TRUE, FALSE), 100, replace = TRUE),
outcome2 = sample(c(TRUE, FALSE), 100, replace = TRUE)
)

dat %>%
gather(key = outcome, value = outcome_value, contains("outcome")) %>%
gather(key = predictor, value = pred_value, gender, ethnicity) %>%
# Statement below creates variable for ordering output
mutate(
pred_ord = factor(interaction(predictor, addNA(pred_value), sep = "_"),
levels = c(paste("gender", levels(addNA(dat$gender)), sep = "_"),
paste("ethnicity", levels(addNA(dat$ethnicity)), sep = "_")))
) %>%
group_by(pred_ord, outcome) %>%
summarise(n = sum(outcome_value, na.rm = TRUE)) %>%
ungroup() %>%
spread(key = outcome, value = n) %>%
separate(pred_ord, c("Predictor", "Pred_value"))

Source: local data frame [9 x 4]

Predictor Pred_value outcome1 outcome2
(chr) (chr) (int) (int)
1 gender Female 25 27
2 gender Male 11 10
3 gender Unknown 12 15
4 ethnicity Maori 10 9
5 ethnicity Pacific 7 7
6 ethnicity Asian 6 12
7 ethnicity Other 10 9
8 ethnicity European 5 4
9 ethnicity Unknown 10 11
Warning message:
attributes are not identical across measure variables; they will be dropped

上表是正确的,因为 Predictor 和 Predictor 值都不是按字母顺序排列的。

编辑

根据要求,这就是使用默认排序(按字母顺序)时产生的结果。这是有道理的,当这些因素组合在一起时,它们会被转换为一个字符变量,并且所有属性都将被删除。
dat %>% 
gather(key = outcome, value = outcome_value, contains("outcome")) %>%
gather(key = predictor, value = pred_value, gender, ethnicity) %>%
group_by(predictor, pred_value, outcome) %>%
summarise(n = sum(outcome_value, na.rm = TRUE)) %>%
spread(key = outcome, value = n)

Source: local data frame [9 x 4]

predictor pred_value outcome1 outcome2
(chr) (chr) (int) (int)
1 ethnicity Asian 6 12
2 ethnicity European 5 4
3 ethnicity Maori 10 9
4 ethnicity Other 10 9
5 ethnicity Pacific 7 7
6 ethnicity Unknown 10 11
7 gender Female 25 27
8 gender Male 11 10
9 gender Unknown 12 15
Warning message:
attributes are not identical across measure variables; they will be dropped

最佳答案

如果您希望您的数据是这样排列的因子,则需要将它们转换回因子,如 gather强制字符(它警告你)。您可以使用 gatherfactor_key要处理的参数 predictor ,但您需要为 pred_value 组装关卡因为它现在结合了原始的两个因素。稍微简化一下:

library(tidyr)
library(dplyr)

dat %>%
gather(key = predictor, value = pred_value, gender, ethnicity, factor_key = TRUE) %>%
group_by(predictor, pred_value) %>%
summarise_all(sum) %>%
ungroup() %>%
mutate(pred_value = factor(pred_value, levels = unique(c(levels_eth, levels_gnd),
fromLast = TRUE))) %>%
arrange(predictor, pred_value)

## # A tibble: 9 × 4
## predictor pred_value outcome1 outcome2
## <fctr> <fctr> <int> <int>
## 1 gender Female 25 27
## 2 gender Male 11 10
## 3 gender Unknown 12 15
## 4 ethnicity Maori 10 9
## 5 ethnicity Pacific 7 7
## 6 ethnicity Asian 6 12
## 7 ethnicity Other 10 9
## 8 ethnicity European 5 4
## 9 ethnicity Unknown 10 11

请注意,您需要使用 uniquefromLast = TRUE将重复的“未知”值排列在正确位置的单个事件中; union会早点放。

关于r - 使用 dplyr tidyr 在汇总表中保留输入变量和因子水平的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39157145/

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