gpt4 book ai didi

r - 如何在 dplyr 链中将数据框与自身连接?

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

有时,我需要在 dplyr 链中将数据框加入(通常是修改后的)自身版本。像这样的东西:

df  <- data.frame(
id = c(1,2,3)
, status = c('foo','bar','meh')
, spouseid = c(4,3,2)
)


df %>%
filter( status == 'foo' | status == 'bar') %>%
# join the filtered table to itself using the dot as the right-hand side
left_join(., by = c('id' = 'spouseid'))

当我尝试时,我得到 Error in is.data.frame(y) : argument "y" is missing, with no default .

最佳答案

问题是使用点只是在左侧移动,所以上面写的方式只将 lhs 传递给 left_join() .要在左侧和右侧都使用点,请使用点两次:

df %>% 
filter( status == 'foo' | status == 'bar') %>%
# the first dot is x argument and the second dot is the y argument
left_join(
x = .
, y = .
, by = c('id' = 'spouseid')
)

这样,您将 lhs 传递给 left_join() 的两个参数。而不是像往常一样依赖 magrittr 的隐含 lhs。

关于r - 如何在 dplyr 链中将数据框与自身连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39495473/

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