Evgenia
2010-Aug-31 08:34 UTC
[R] Export Results to a txt file applying a function to a list
Dear users, *******I have a function f to simulate data from a model (example below used only to show my problem) f<-function(n,mean1){ a<-matrix(rnorm(n, mean1 , sd = 1),ncol=5) b<-matrix(runif(n),ncol=5) data<-rbind(a,b) out<-data out} *********I want to simulate 1000 datasets (here only 5) so I use S<-list() for (i in 1:5){ S[[i]]<-f(n=10,mean1=0)} ******I have a very complicated function for estimation of a model which I want to apply to Each one of the above simulated datasets fun<-function(data){data<-as.matrix(data) sink(' Example.txt',append=TRUE) cat("\n***********************\nEstimation \n********************\nDataset Sim : ", i ) d<-data%*%t(data) s<-solve(d) print(s) out<-s out } ######First using for I take my results (for some element of a list I can't have results due to invertibility or convergence problem helpful suggestion of Joris Meys ) results<-list() ffor(i in 1:5){ tmp <- try(fun(data=S[[i]])) results[[i]] <- ifelse(is(tmp,"try-error"),NA,tmp) } #####and so I can see using "cat" the results for each dataset. #####I want to use lapply at the last step istead of for to make faster my program results <- lapply(S,function(x){ tmp <- try(fun(data=x)) ifelse(is(tmp,"try-error"),NA,tmp) } ) ##### My question is how can I export results to a file knowing exactly for which of the element of the list these are (for some element of a list I can't have results due to invertibility or convergence problem) Thanks alot Evgenia -- View this message in context: http://r.789695.n4.nabble.com/Export-Results-to-a-txt-file-applying-a-function-to-a-list-tp2401218p2401218.html Sent from the R help mailing list archive at Nabble.com.