[SOLVED] Error in genereting_fuction(100) : could not find function "genereting_fuction

Issue

I created this function to generate the data with the characteristics I need:

genereting_fuction<-function(n){
  
  X1=rnorm(n)+mean_shifts[1]
  X4=rnorm(n)+mean_shifts[4]
  X2=X1*p12+std_e2*rnorm(n)+mean_shifts[2]
  X3=X1*p13+X4*p43+std_e3*rnorm(n)+mean_shifts[3]
  X5=X2*p25+X3*p35+std_e5*rnorm(n)+mean_shifts[5]
  sample=cbind(X1,X2,X3,X4,X5)
  return(sample)
}

if I call it for a single item it works but when I call it in the applay function as follows:

dati<-lapply(1:100, genereting_fuction(100))

I get this error:
Error in genereting_fuction(100) :
could not find function "genereting_fuction"

Solution

Note that I prefer the replicate solution by @Jakub.Novotny for your purpose, but to understand what went wrong using lapply, this is why and how to solve it.

Using apply and a function, it assumes x the value of your apply to be provided always in the function.

To make it work you can do two things.

  1. lapply(1:100, function(x) genereting_fuction(100))

  2. include x in your function like genereting_fuction <- function(x, n) { # code here } and then you can use lapply(1:100, genereting_fuction, n = 100)

Answered By – Merijn van Tilborg

Answer Checked By – Mildred Charles (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *