Goal is to perform rowwise simulation and export the results.
Consider a simple example:
library(tidyverse)
tf <- tibble(a=c(-1,1),b=c(1,2)) ## parameters for simulation
tf1 <- tf %>% rowwise %>% mutate( ## simulate 3 values each row
c=list(rnorm(3,a,b)) ## results need to save in a list
)
print(tf1$c)
As shown here, tf1$c is a list of length 2, each includes 3 numeric values.
The simulation can be otherwise done using a for loop:
tf2 <- matrix(NA,2,3)
for(i in 1:2){
tf2[i,] <- rnorm(3,tf2$a[i],tf2$b[i])
}
How to convert tf1$c into tf2? Any help is appreciated!