我正在尝试为ID求位,而B的在这些位之内为ID。因此,B的四分位数将取决于A的十分位数。以下是我的尝试:
require(dplyr)
require(OneR)
foo <- data.frame(ID = 1:100,
A = runif(100, 50, 200),
B = runif(100, 50, 200),
stringsAsFactors = FALSE)
foo1<-foo %>%
mutate(Aquantile = bin(A,nbins = 10,labels = c(1:10))) %>%
group_by(Aquantile) %>%
mutate(Bquantile = bin(B,nbins = 4,labels = c(1:4)))
foo1 <- foo1 %>% mutate(checkB = bin(B,nbins = 4,labels = c(1:4)))
但是,如检查变量所示,Bquantile中的结果独立于Aquantile。请帮助
mutate()
不会抵消分组。因此,在第二步中您将获得相同的结果。当不提供特定标签时,您可以猜测各组之间的差异
foo1 <- foo %>%
mutate(Aquantile = bin(A, nbins = 10, labels = c(1:10))) %>%
group_by(Aquantile) %>%
mutate(Bquantile = bin(B, nbins = 4))
is_grouped_df(foo1)
# [1] TRUE
foo1 %>%
mutate(checkB_grouped = bin(B, nbins = 4, labels = c(1:4))) %>%
ungroup() %>%
mutate(checkB_not_grouped = bin(B, nbins = 4, labels = c(1:4)))