how to retrieve multiple named subcomponents (as.list) of a function applied to a dplyr grouped data and save them in multiple variables in one go?

I want to apply normalmixEM function in R to every group of data using dplyr group_by function and retreive some of the results variables in related column names at one go. I can do this by applying normalmixEM as many time as I want to retrieve a single result and save it to a column using mutate function but I want to apply normalmixEM only once and retrieve all the needed results to the intended columns.

I have tried the following which is not what I want to do:

    data2 = data1 %>% 
      group_by(criteria1, criteria2) %>% 
      mutate(mu1 = normalmixEM(Value)$mu[1], 
             mu2 = normalmixEM(Value)$mu[2],
         lambda1 = normalmixEM(Value)$lambda[1],
         lambda2 = normalmixEM(Value)$lambda[2])

I want to apply normalmixEM only once per group and save the results to the intended new cols then go to the next group.

Leave a Comment