In short, you really need to study the Help files carefully for both, as you are using both tapply and lapply incorrectly. If that doesn't work, I think you should spend some time with one of the many excellent R tutorials on the web. You need to beef up your understanding of the syntax.
But briefly: 1) the tapply call must be of the form tapply(x, fac, fun) where x is atomic (i.e. a vector) and fac is a factor that splits x into groups. You have obviously got the call all wrong. 2) And in the second, X must be a list or vector, not a matrix and the function needs to have its elements as an argument, something like FUN = function(x)FUN1(x, ... your other arguments...) Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Fri, Mar 10, 2017 at 2:40 PM, Fadhah <fadeh2...@gmail.com> wrote: > 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]] > > ______________________________________________ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.