0

I have some data that consists in 3 response variables divided in 3 groups tested in several batches . e.g:

Batch    Type    Replicate    Y1     Y2    Y3
1         A        a           200    100   80

I need to extract the variances for that I build a function:

My_function <- function(x) {
  md_mm <- lmer(Y1 ~ (1|Batch), data = x) 
  tab_mm <- summary(md_mm)
  tab2_mm <- as.data.frame(tab_mm$varcor)
  m <- mean(x$Y1,na.rm=TRUE)
  BatchVar <-tab2_mm[1,4]
  ReptVar <- tab2_mm[2,4]
  df_mm <- data.frame(m,BatchVar, ReptVar)
  return(df_mm)
}

and then I use it:

    table <- df %>% 
  group_by(Type) %>% 
  do(My_function(.)) %>% 
  as.data.frame()

So far this works, but I have been trying to change it so I can get it so the table includes the results for all the variables. I tried with a function with My_function(x,y) and then map() so it runs on all the variables and it works if I tried with the whole variable, when I try to group it then if goes wrong. If anyone has an idea that could help, it would be very much appreciated. Thanks,

user51962
  • 1
  • 1
  • Hi user51962, welcome to Stack Overflow. It will be much easier for us to help you if you provide a generous sample of `df` with `dput(df)` or if your data is very large `dput(df[1:30,])`. You can edit your question and paste the output. You can surround it with three backticks (```) for better formatting. See [How to make a reproducible example](https://stackoverflow.com/questions/5963269/) for more info. – Ian Campbell May 13 '20 at 14:00

1 Answers1

0

I created a data, trying to match the one you posted:

library(tidyverse)

set.seed(123)
df <- data.frame(
  batch =  sample(c(1, 2), 100, replace = TRUE), 
  type = sample(c("A", "B"), 100, replace = TRUE), 
  replicate = sample(c("A", "B"), 100, replace = TRUE), 
  y1 = rnorm(100), 
  y2 = rnorm(100),
  y3 = rnorm(100))

Now, you can use group_by and summarise_if to calculate by group only the numeric variables (you can replace var(x) with your actual function):

df %>% 
  group_by(batch, type, replicate) %>%
  summarise_if(is.numeric, function(x) {var(x)})

Results:

 batch type  replicate    y1    y2    y3
  <dbl> <fct> <fct>     <dbl> <dbl> <dbl>
1     1 A     A         0.766 1.68  2.02 
2     1 A     B         0.908 0.917 1.29 
3     1 B     A         0.979 1.53  0.567
4     1 B     B         0.911 0.727 1.33 
5     2 A     A         0.681 1.16  0.871
6     2 A     B         0.627 0.727 0.781
7     2 B     A         0.688 0.505 1.16 
8     2 B     B         1.98  0.890 0.890
DJV
  • 4,743
  • 3
  • 19
  • 34
  • Thanks for the reply, I think summarize only works for predefined functions like the one in your example. Mine was made so it doesn't work. – user51962 May 15 '20 at 11:55
  • You're welcome, however, I think it's not correct. The `function(x)` argument can take any functions. You can find here more info https://dplyr.tidyverse.org/reference/summarise_all.html – DJV May 16 '20 at 20:57