[R] Help with lapply and tapply

Fadhah fadeh2013 at gmail.com
Fri Mar 10 23:40:07 CET 2017


Dear all, 

Thank you in advance for your time and help. I quite new to R and face a problem with lapply and tapply functions. 
I simulated data and run the simulation 10 times to get 10 different simulated data. I have also built up my function and would like to apply this function to these 10 different data without repeating the code for 10 times. I now that we can use a loop family functions in R such as lapply or tapply functions. I tried both of them but both of them did not work. My data was stored as vector mode list. 

Here is my data:

library(VineCopula)
library(copula) 
Runs= 10 Saveas = vector(mode = "list", length = Runs) 
 pb <- txtProgressBar(min = 0, max = Runs, style = 3) 
for(j in 1:Runs){ 
 setTxtProgressBar(pb, j)
N=2000 
 dim=dim 
 U=runif(N, min=0,max=1) 
 X = matrix(NA, nrow=N, ncol=2)
inds <- U < 0.7 
 X[inds, ] <- rCopula(sum(inds), claytonCopula(1, dim=2)) 
 X[!inds, ] <- rCopula(N - sum(inds), frankCopula(4, dim=2)) 
Saveas[[j]] = X }
Then I built my function. I would like to apply this function to the 10 simulation run. That is I have 10 simulated data and would like to run my function to these data. I tried lapply and tapply function but I got  errors.
This is my function:

FUN1 <- EM_mixture_copula(data = Saveas[[j]],pi_1=pi_1,pi_2=pi_2,theta = theta,                Theta=Theta, tol = .00001, maxit = 1000)
Here is my tries with the errors that I got:

> result <- tapply(X,FUN1,simplify = T)
Error in tapply(X, FUN, simplify = T) : arguments must have same length.

> Result <– lapply(X,FUN1)
Error in get(as.character(FUN), mode = "function", envir = envir) : object 'F' of mode 'function' was not found.



Once I got the result, I would like to have a summary statistics of my function for each run. So, can I use 

Summary(result) ?

Any help, please?
Kinds regards, 
Fadhah

Sent from my iPhone
	[[alternative HTML version deleted]]



More information about the R-help mailing list