I am running some simulations in R using the repeat{} function, for some contextual reasons I am not able to convert the block of code to something that uses apply functions (or something more effective/efficient). I have output I am saving from the repeat{} function iteratively into a list(), using the following code:
results <- list()
sim <- list()
reps <- 5
n1 <- 5
n2 <- 5
low_eqbound_d <- -.3
high_eqbound_d <- .3
count <- 0
repeat {
x <- rnorm(n1, 0, 2)
y <- rnorm(n2, 2, 2)
print(mid <- mean(x)-mean(y))
sdpooled <- sqrt((((n1 - 1)*(sd(x)^2)) + (n2 - 1)*(sd(y)^2))/((n1 + n2) - 2))
low_eqbound <- low_eqbound_d*sdpooled
high_eqbound <- high_eqbound_d*sdpooled
if (mid < low_eqbound & mid > high_eqbound) {
next
}
if (mid >= low_eqbound & mid <= high_eqbound) {
sim <- TOST(m1=mean(x), m2=mean(y), sd1=sd(x), sd2=sd(y), n1=n, n2=n,
low_eqbound_d=-0.3, high_eqbound_d=0.3)
results <- append(results, sim)
count <- count+1
}
if (count == 5) {
break
}
}
results1 <- as.data.frame(results)
The list looks like this at the end:
I want to convert this to a data.frame for further analysis/visualisation. Note that there are 5 pieces of data I need extracted from the list, and this repeats along the entire object. I want to create a data.frame with 5 columns, and take each entry from the list and put it into each column of the data.frame, repeating this for all repetitions in my code. I have attempted to use as.data.frame()
results1 <- as.data.frame(results)
But get this:
Any help would be fantastic!


You can first rename the column names for consistency throughout, and then use the pattern to pivot the data to long format:
The
idcolumn can then be removed as you wish.