0

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))

This code works. However,

example.df %>% 
   filter(treated=="No") %>% 
   {pairw.kw("var1", "species",conf = 0.95)}

gives me the error message

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.

morgan121
  • 2,213
  • 1
  • 15
  • 33
jaydoc
  • 79
  • 1
  • 7

1 Answers1

1

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`)
  )
Paweł Chabros
  • 2,349
  • 1
  • 9
  • 12
  • Thanks. That worked. I did come across the %$% operator, but did not realize that it exposes the variables within a dataframe. – jaydoc Feb 26 '19 at 16:43
  • If I use group instead of filter for this operation no grouping actually takes place. Would it be possible to group by (treated) and pass each tibble to pairw.kw one after another for analysis? – jaydoc Feb 27 '19 at 22:40
  • 1
    Yes. See my edited answer. Now you have comparisons in `kw` column, and I made `p_val` column to show you how to pull specific statistics. – Paweł Chabros Feb 28 '19 at 06:09
  • Edit: When i examined the dataframe closely, the results are the same as `by(example.df,example.df$treated, function(X) pairw.kw(X$var1, X$species, conf = 0.95))`. One final request: If I had a dataframe with several more variables, how could I loop through them all using similar code? Using say, `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), resp=rnorm(50, 10,5), effect = rnorm (50, 25, 5))` – jaydoc Mar 02 '19 at 04:39