Here is my real data. Group A is my reference and I want to compare the other groups to group A (dunnett analysis). I want to simulate another 5 random numbers for Group A based on the mean and std for my current Group A. and use the combined the data to do the analysis.

Group Y

A 30.17

B 30.65

A 30.35

C 30.57

D 30.75

A 30.52

E 30.05

F 30.56

G 30.86

A 30.89

H 30.69

I 31.19

J 30.68

K 30.84

A 30.82

B 24.13

C 24.5

D 24.28

Here is my R code:

library(readxl)

library(emmeans)

# import the real data

data <- read_excel("Path/Sample.xlsx")

View(data)

A_sd<-sd(data[which(data$Group=="A"),]$Y)

A_mean<-mean(data[which(data$Group=="A"),]$Y)

# do 100 times Simulations

sim=100

# create vectors to save the anona results.

res = vector("list",sim)

# Create data frames for the simulated the data

df_sim<- replicate(n=sim,data.frame(Group = rep('A',5), Y=rep(0,5)),simplify=FALSE)

# create data frames for the combined data( real data merged with simulated data)

df_merge<-replicate(n=sim,data.frame(Group=rep(NA,23),Y=rep(0,23)),simplify=FALSE)

for (i in 1:sim)

{

Y<-rnorm(n=5,A_mean,A_sd)

df_sim[[i]]$Y[which(df_sim[[i]]$Group=='A')]<-Y

df_merge[[i]]<-rbind(data,df_sim[[i]])

df_merge[[i]]$Group<-as.factor( df_merge[[i]]$Group)

res[[i]]<- lm(df_merge[[i]]$Y ~ df_merge[[i]]$Group, data = df_merge[[i]])

summary(res[[i]])

#Dunnect analysis comparing other groups to group A

# got error df_merge[[i]]Group : operator is invalid for atomic vectors

lssquare[[i]]=emmeans(res[[i]],specs=trt.vs.ctrlk~Group)

}

The code works well until I do the Dunnnet analysis using the emmeans function. I got the error df_merge[[i]]Group : operator is invalid for atomic vectors.

If I change my code to lssquare[[i]]=emmeans(res[[i]],specs=trt.vs.ctrlk~getElement(df_merge[[i]],"Group"),data=df_merge[[i]])

i got the error:Error in `[.data.frame`

(tbl, , vars, drop = FALSE) :

undefined columns selected

Any ideas on how to fix this error? Appreciated for anly help