gpt4 book ai didi

r - dplyr 自连接与过滤器

转载 作者:行者123 更新时间:2023-12-03 09:17:54 24 4
gpt4 key购买 nike

我想从长格式数据框中所有其他标记的项目中减去带有标签“基线”的行中的值。使用带有“基线”子集的 left_join 可以很容易地通过两个步骤来完成此操作。但是,我不知道如何将 vas_1 和 vas_diff 组合成一个链。

library(dplyr)
# Create test data
n_users = 5
vas = data_frame(
user = rep(letters[1:n_users], each = 3),
group = rep(c("baseline", "early", "late" ),n_users),
vas = round(rgamma(n_users*3, 10,1.4 ))
)
# The above data are given


# Assume some other operations are required
vas_1 = vas %>%
mutate(
vas = vas * 2
)
# I want to put the following into one
# chain with the above
# Use self-join to subtract baseline
vas_diff = vas_1 %>%
filter(group != "baseline") %>%
# Problem is vas_1 here. Using . gives error here
# Adding copy = TRUE does not help
# left_join(. %>% filter(group == "baseline") , by = c("user")) %>%
left_join(vas_1 %>% filter(group == "baseline") , by = c("user")) %>%
mutate(vas = vas.x - vas.y) %>% # compute offset
select(user, group.x, vas) # remove temporary variables

vas_diff

最佳答案

. 应该多次使用时,我使用匿名函数:

... %>% (function(df) { ... }) %>% ...

因此,就您而言:

vas_diff = vas_1 %>%
filter(group != "baseline") %>%
(function(df) left_join(df, df %>% filter(group == "baseline") , by = c("user"))) %>%
mutate(vas = vas.x - vas.y) %>% # compute offset
select(user, group.x, vas)

(这不会产生上面评论中描述的理想结果,但它展示了如何使用匿名函数)

但你可能想要这个:

vas_diff = vas_1 %>%
left_join(
x = filter(., group != "baseline")
, y = filter(., group == "baseline")
, by = c("user")
) %>%
mutate(vas = vas.x - vas.y) %>% # compute offset
select(user, group.x, vas) # remove temporary variables

关于r - dplyr 自连接与过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35744164/

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