Replacement has 6 rows, data has 2

what is wrong with my code

set.seed(1)
d <-
tibble(group = rep(c("normal","covid1","covid2",
                     "covid3", "covid4"), each = n)) %>%
mutate(treatment = rep(c(0,1,2,3,4), each=n),
       y = c( group=="normal" ~(rnorm(n, mean = mu_n, sd =1)),
       group=="covid1" ~(rnorm(n, mean = mu_c1, sd =1)),
       group=="covid2" ~(rnorm(n, mean = mu_c2, sd =1)),
       group=="covid3" ~(rnorm(n, mean = mu_c3, sd =1)),
       group=="covid4" ~(rnorm(n, mean = mu_c4, sd =1))))

I got an error
I want to settle the value of y to be rnorm distribution with different means depending on the group

Error in `$<-.data.frame`(`*tmp*`, "call_text", value = c("... %>% ...", : 
replacement has 6 rows, data has 2

Is there more to your code? I am able to run it just fine with no issues:

# Assuming you are using these packages
library(tibble)
library(dplyr)

d <- tibble(group = rep(c("normal","covid1","covid2",
                          "covid3", "covid4"), each = n)) %>%
    mutate(treatment = rep(c(0,1,2,3,4), each=n),
           y = c( group=="normal" ~(rnorm(n, mean = mu_n, sd =1)),
                  group=="covid1" ~(rnorm(n, mean = mu_c1, sd =1)),
                  group=="covid2" ~(rnorm(n, mean = mu_c2, sd =1)),
                  group=="covid3" ~(rnorm(n, mean = mu_c3, sd =1)),
                  group=="covid4" ~(rnorm(n, mean = mu_c4, sd =1))))
d
#> # A tibble: 5 x 3
#>   group  treatment y        
#>   <chr>      <dbl> <list>   
#> 1 normal         0 <formula>
#> 2 covid1         1 <formula>
#> 3 covid2         2 <formula>
#> 4 covid3         3 <formula>
#> 5 covid4         4 <formula>

Created on 2022-07-12 by the reprex package (v1.0.0)

1 Like

I think this is the old mutate() behavior, maybe you just need to update your dplyr version.

2 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.