英文:
Is there a quicker way to factor and relevel a column based on the levels in another column?
问题
你的代码如下所示可以运行,但我觉得有一种更快的方法来实现这个,但我不知道有没有更好的函数。当我在论坛上搜索基于另一列进行重新分级时,我只能找到关于factor(metric, levels = c(...)
的答案 - 这是通过一系列调用完成的 - 但我觉得有一种更优雅的解决方案。
# 我想在ggplot中显示的星期顺序
week_order <- c('Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'Mon', 'Tue')
# 数据
df <- tibble(metric = c(0, 8, 12, 18, 6, 12, 20),
label_day = c('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'),
legend_text = c('Off', 'ReEntry', 'Strength', 'Match 1', 'Recovery', 'Activation', 'Match 2'))
# 将`label_day`转换为因子,并根据`week_order`排序
df <- df %>%
mutate(label_day = factor(label_day, levels = week_order)) %>%
arrange(label_day)
# 有没有更优雅的方法来做这个? -------------------------------------------------------
# 现在df已经按正确顺序排列,提取`legend_text`的顺序以在ggplot中使用
legend_text_order <- df %>%
distinct(legend_text) %>%
pull()
# 将`legend_text`转换为因子,并根据`legend_text_order`排序
df <- df %>%
mutate(legend_text = factor(legend_text, levels = legend_text_order))
# ------------------------------------------------------------------------------------
# 绘图
ggplot(df, aes(x = legend_text, y = metric)) +
geom_col()
希望这有助于你更优雅地完成相同的任务。
英文:
My code below works, but I feel like I'm missing a way to do this quicker (ie I don't know a better function). When I search forums for relevel based on another column all I am given are answers around factor(metric, levels = c(...)
- which I've done through a series of calls - but I feel like there is a more elegant solution.
# Order of the week I would like to display in ggplot
week_order <- c('Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'Mon', 'Tue' )
# Data
df <- tibble(metric = c(0, 8, 12, 18, 6, 12, 20),
label_day = c('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'),
legend_text = c('Off', 'ReEntry', 'Strength', 'Match 1', 'Recovery', 'Activation', 'Match 2'))
# Change `label_day` into a factor and order based on `week_order`
df <- df |>
mutate(label_day = factor(label_day, levels = week_order)) |>
arrange(label_day)
# Is there a more elegant way to do this ? -------------------------------------------------------
# Now that the df is in the correct order, pull out the order that `legend_text` is in to use in ggplot
legend_text_order <- df |>
distinct(legend_text) |>
pull()
# Change `legend_text` into a factor and order based on `legend_text_order`
df <- df |>
mutate(legend_text = factor(legend_text, levels = legend_text_order))
#-------------------------------------------------------------------------------------------------
# Plot
ggplot(df, aes(x = legend_text, y = metric)) +
geom_col()
答案1
得分: 1
你可以在一个管道中完成所有操作:
df <- df %>%
mutate(label_day = factor(label_day, levels = week_order)) %>%
arrange(label_day) %>%
mutate(legend_text = factor(legend_text, levels = unique(legend_text)))
而且由于你实际上不需要将 label_day
保持为因子,只需使用它的顺序来排列数据集,你可以进一步简化它:
df <- df %>%
arrange(factor(label_day, levels = week_order)) %>%
mutate(legend_text = factor(legend_text, levels = unique(legend_text)))
输出:
英文:
You can do it all in one pipe:
df <- df |>
mutate(label_day = factor(label_day, levels = week_order)) |>
arrange(label_day) |>
mutate(legend_text = factor(legend_text, levels = unique(legend_text)))
And as you don't actually need keep label_day
as a factor, just use it's order to arrange the dataset, you can shrink it a little further:
df <- df |>
arrange(factor(label_day, levels = week_order)) |>
mutate(legend_text = factor(legend_text, levels = unique(legend_text)))
Output:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论