# [SOLVED] User defined function “`R“` fails to run

## Issue

I would to learn to how to create function in `R` for simulating data based on the original data. I have access to toy data from (which is confidential) and I am not allowed to share. At the same time, data is to big to share.

1. Data is similar as below but I reduced the size substantially.
``````epsilon=2
rs=matrix(rnorm(n=10000),nrow=10000, ncol=100)
ival=matrix(rnorm(n=10, mean=50, sd=30), nrow=10, ncol=100)
r1= matrix(c(5.5,5.7, 5, 6, 6.5, 7, 6.8, 7.4,8,10), nrow=10, ncol=1)
r2= matrix(c(6.5,7, 6.5, 8, 10, 13, 13, 15,18,20), nrow=10, ncol=1)

``````
1. Based on above data, now I would like to simulate to generate new data. Therefore, I created following function to do so. But it doesn’t seem to run well. Your suggestions and help will be highly appreciated
``````toyfunction = function(epsilon, rs, ival, r1, r2 ){
toydata=matrix(0, nrow=10000,ncol=100)
r=matrix(0,nrow=10,ncol=100)
r[1:10,]=5

for(i in 1:1000){
for(col in 1:100){
#1-5 rows
for(row in 1:5){
toydata[row+(i-1)*10,col]=ival[row, col]+rs[row+(i-1)*10,col]
}
#6-10 rows
for(row in 6:10){
toydata[row+(i-1)*10,col]=toydata[row-5+(i-1)*10,col] + epsilon*log(r[row-1,1]/r[row-10,1]) + rs[row+(i-1)*10,col]

if(toydata[row+(i-1)*10,col] < 45){
r[row,col]=r1[row,1]
}
else{
r[row,col]=r2[row,1]
}
}
}
}
}

mydata=toyfunction(epsilon,rs, ival,r1, r2)

``````

I will be highly grateful for your help and suggestions. I am also assuming that function will give me output in matrix. This is also crucial for me as I will have to do some matrix algebra later for the analysis.

## Solution

`epsilon*log(r[row-1,1]/r[row-10,1])`