为什么我的dplyr group_by和总结不正常工作? (带有plyr的名称 - collision)

问题描述 投票:0回答:4

等。我想通过药物

DRUG
和FED状态概述一些有关AUC,TMAX和CMAX的统计数据。我使用dplyr。例如:对于AUC:

FED

nove,输出不被药物分组并喂食。它仅提供一条包含所有统计数字的线,但没有在毒品和喂养上。
任何想法为什么?我该如何做正确的事?
    

我相信您已经在
dplyr

之后加载了

Plyr

,这就是为什么您要获得总体摘要而不是分组摘要的原因。

这是
r plyr dplyr shadowing name-collision
4个回答
204
投票
加载最后的事情。

CI90lo <- function(x) quantile(x, probs=0.05, na.rm=TRUE) CI90hi <- function(x) quantile(x, probs=0.95, na.rm=TRUE) summary <- df %>% group_by(DRUG,FED) %>% summarize(mean=mean(AUC0t, na.rm=TRUE), low = CI90lo(AUC0t), high= CI90hi(AUC0t), min=min(AUC0t, na.rm=TRUE), max=max(AUC0t,na.rm=TRUE), sd= sd(AUC0t, na.rm=TRUE)) 现在删除Plyr,然后重试,您将获得分组的摘要。

library(dplyr) library(plyr) df %>% group_by(DRUG,FED) %>% summarize(mean=mean(AUC0t, na.rm=TRUE), low = CI90lo(AUC0t), high= CI90hi(AUC0t), min=min(AUC0t, na.rm=TRUE), max=max(AUC0t,na.rm=TRUE), sd= sd(AUC0t, na.rm=TRUE)) mean low high min max sd 1 150 105 195 100 200 50

Aosmith答案的变体可能会帮助某些人。指导R直接调用Dplyr的功能。当一个包裹干扰另一个包装时,这是个好诀窍。

detach(package:plyr)
df %>%
      group_by(DRUG,FED) %>%
      summarize(mean=mean(AUC0t, na.rm=TRUE), 
                low = CI90lo(AUC0t), 
                 high= CI90hi(AUC0t),
                 min=min(AUC0t, na.rm=TRUE),
                 max=max(AUC0t,na.rm=TRUE), 
                 sd= sd(AUC0t, na.rm=TRUE))

Source: local data frame [4 x 8]
Groups: DRUG

  DRUG FED mean low high min max  sd
1    0   0  150 150  150 150 150 NaN
2    0   1  NaN  NA   NA  NA  NA NaN
3    1   0  100 100  100 100 100 NaN
4    1   1  200 200  200 200 200 NaN

在DPLYR中,用户经常使用

Ggplot
以及
GGPUBR

38
投票
dplyr

有一些不兼容。以同样的方式,如上所示,您可以使用

dplyr::package
,但是如果它不断起作用,就像我发生在我身上一样,只要拆下库就足够了,

6
投票

或您可以考虑使用detach("package:ggpubr", unload = TRUE) df %>% dplyr::group_by(DRUG,FED) %>% dplyr::summarize(mean=mean(AUC0t, na.rm=TRUE), low = CI90lo(AUC0t), high= CI90hi(AUC0t), min=min(AUC0t, na.rm=TRUE), max=max(AUC0t,na.rm=TRUE), sd= sd(AUC0t, na.rm=TRUE)) data.table

TrysSQLDF是最好的方法,并且可以易于学习分组数据。
以下是您需要的示例。所有类型的数据示例分组SQLDF库非常有帮助。

library(data.table) setDT(df) # set the data frame as data table df[, list(mean = mean(AUC0t, na.rm=TRUE), low = CI90lo(AUC0t), high = CI90hi(AUC0t), min = as.double(min(AUC0t, na.rm=TRUE)), max = as.double(max(AUC0t, na.rm=TRUE)), sd = sd(AUC0t, na.rm=TRUE)), by=list(DRUG, FED)] # DRUG FED mean low high min max sd # 1: 1 0 100 100 100 100 100 NA # 2: 1 1 200 200 200 200 200 NA # 3: 0 1 NaN NA NA Inf -Inf NA # 4: 0 0 150 150 150 150 150 NA # Warning messages: # 1: In min(AUC0t, na.rm = TRUE) : # no non-missing arguments to min; returning Inf # 2: In max(AUC0t, na.rm = TRUE) : # no non-missing arguments to max; returning -Inf

3
投票

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.