dplyr,dunn测试,dim(robj)<-c(dX,dY)中的错误:dims [产品0]与对象的长度不匹配 [英] dplyr, dunn test, Error in dim(robj) <- c(dX, dY) : dims [product 0] do not match the length of object
问题描述
我正试图将通过变量值过滤的数据集从R中的 asbio包传递给pairw.kw函数。
I am trying to pass a dataset filtered by a variable value to the pairw.kw function from the "asbio" package in R.
example.df <- data.frame(
species = sample(c("primate", "non-primate"), 50, replace = TRUE),
treated = sample(c("Yes", "No"), 50, replace = TRUE),
gender = sample(c("male", "female"), 50, replace = TRUE),
var1 = rnorm(50, 100, 5)
)
library(dplyr)
library(asbio)
with(example.df, pairw.kw(var1, species, conf=0.95))
此代码有效。但是,
example.df %>%
filter(treated=="No") %>%
{pairw.kw("var1", "species",conf = 0.95)}
提供错误消息
dim(robj)中的错误<-c(dX,dY):
dims [产品0]与对象[1]的长度不匹配。
Error in dim(robj) <- c(dX, dY) : dims [product 0] do not match the length of object [1]
除了假设应用滤波器后,被比较的两个向量的长度变得不同。
I cannot understand what is causing this, other than to assume that the two vectors being compared become different lengths after the filter is applied.
除了将数据显式设置为新数据框并使用它之外,是否有其他方法可以解决此问题?我知道这行得通,但想知道是否存在更优雅的解决方案。
Is there a way to fix this other than subsetting the data explicitly to a new dataframe and using that instead? I know that will work, but wanted to know if a more elegant solution exists.
推荐答案
首先,%>%
管道传递数据。框架作为第一个参数到 pairw.kw
函数。其次, pairw.kw
函数需要两个向量作为输入。您可以使用 magrittr
包中的%$%
管道实现此目标。它的工作方式类似于具有$code>功能的。
First of all %>%
pipe passes a data.frame to the pairw.kw
function as a first argument. Secondly, pairw.kw
function wants two vectors as an input. You can achive this with %$%
pipe from magrittr
package. It works similar to with
function.
library(magrittr)
example.df %>%
filter(treated=="No") %$%
pairw.kw(var1, species, conf = 0.95)
要回答的问题:
Answer to question in comment:
library(tidyverse)
library(magrittr)
library(asbio)
example.df %>%
group_by(treated) %>%
nest() %>%
mutate(
kw = map(
data,
~ .x %$% pairw.kw(var1, species, conf = 0.95)
),
p_val = map_dbl(kw, ~ .x$summary$`Adj. P-value`)
)
这篇关于dplyr,dunn测试,dim(robj)<-c(dX,dY)中的错误:dims [产品0]与对象的长度不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!