0

I have been getting error messages when trying to do hypothesis tests, like T or Wilcox tests, on nested data. This is all code that previously worked until recently. (I wonder if it could be related to the dplyr .7 update?) For example,

ID <- 1:100
group <- as.character(rep(c('w', 'x', 'y', 'z'),25))
factor <- as.factor(rep(c('a', 'b'), 50))
num <- runif(100)
df<- data.frame(ID, group, factor, num)

dfTtest <- df %>% 
nest(-group) %>% 
mutate(p = map_dbl(data, ~t.test(.$num~.$factor)$p.value))

Gives me the error:

Error in mutate_impl(.data, dots) : 
  Evaluation error: invalid type (NULL) for variable '.$num'.
In addition: Warning messages:
1: Unknown or uninitialised column: 'num'. 
2: Unknown or uninitialised column: 'factor'. 

How can I fix this?

Thank you!

user42485
  • 751
  • 2
  • 9
  • 19
  • What is `sample` in your `data.frame`? (Note that `sample` is also a base R function.) Did you not publish this data? – CPak Jun 27 '17 at 14:26
  • Oops! 'Sample' can be anything. Just a name to identify the individual rows. Let's call it "ID" instead. – user42485 Jun 27 '17 at 14:43
  • 1
    A work-around if you are using a formula is to make use of the `data` argument, `mutate(p = map_dbl(data, ~t.test(num ~ factor, data = .x)$p.value) )`. To run this with your example dataset you need `factor <- as.factor(rep(c('a', 'b'), each = 50))`. – aosmith Jun 27 '17 at 18:30
  • Thanks, @aosmith. That did work. Do you know why my syntax didn't work? – user42485 Jun 27 '17 at 19:32
  • I don't know why. It seems like it has something to do with using formulas with dollar sign notation. For example, using `~t.test(as.formula(".$num ~ .$factor"))$p.value` also seems to work. – aosmith Jun 27 '17 at 21:02

0 Answers0