Repeating a complicated sequence and putting the output in a new dataframe in R -


i'm relatively new @ r, , i've been working while piece code let me compare 6 variables 2 populations drawn large data frame (consisting of original data sampled 1000x replacement based on pop size).

here's example of data:

head(phenom2)  id         name    phenonames group   hml    rml    fml    tml   fhd   bib 1 378607 paleoaleut paleoaleut     1  323.5 248.75 434.50 355.75 46.84  na 2 378664 paleoaleut paleoaleut     1   na   238.50 441.50 353.00 45.83 277.0 3 378377 paleoaleut paleoaleut     1  309.5 227.75 419.00 332.25 46.39 284.0 4 378463 paleoaleut paleoaleut     1  283.5 228.75 397.75 331.00 44.37 255.5 5 378602 paleoaleut paleoaleut     1  279.5 230.00 393.00 329.50 45.93 265.0 6 378610 paleoaleut paleoaleut     1  307.5 234.25 419.50 338.50 43.98 271.5 

my entire code, initial resampling loop output follows:

samplesm <- ddply(phenom2, .(group), function(d) d[sample(1:nrow(d), replace = true),])  n <- 1000 samplesm <- vector("list", n) (i in seq_along(samplesm))  samplesm[[i]] <- ddply(phenom2, .(group),     function(d) d[sample(1:nrow(d), replace = true),])  all.samplesm <- do.call(rbind, samplesm)  covdatam <- all.samplesm[,4:10]  covm <- with(covdatam, by(covdatam[,2:7], covdatam$group, cov, use = "pairwise.complete.obs", method = "pearson"))  cov.aggregator <- function(k){   out <- array(na,c(6,6,dim(k)))  for(i in 1:dim(k)){  out[,,i] <- k[[i]]  }  return(out) }   testmatrix<-cov.aggregator(covm)   covm.pooled<-apply(testmatrix,c(1,2), mean, na.rm = t)   male.means<-aggregate(all.samplesm[,4:10], = list(all.samplesm$"group"), fun = mean, na.rm = t)   cov.mstd.aggregator <- function(k,means){   out <- array(na,c(6,6,dim(k)))  for(i in 1:dim(k)){  mi <- as.matrix(means[i,]) out[,,i] <- k[[i]]/(mi%*%t(mi))  }  return(out) }   male.p.mstd <- cov.mstd.aggregator(covm,as.matrix(male.means[,3:8]))   covm.mstd.pooled <- apply(male.p.mstd,c(1,2),mean, na.rm = t)  pooledmmean <- apply(male.means[,3:8],2,mean,na.rm=t)  pointhope<-male.means[7,3:8]  kikuyu<-male.means[128,3:8]  deltam1<-(pointhope - kikuyu)/pooledmmean  pinv <- ginv(covm.mstd.pooled)  b.kikuyu.pointhope <- pinv %*% as.matrix(t(deltam1)) 

i know unwieldy, works produce matrix of 6 variables i'm interested in (the hml, rml, fml, tml, fhd, bib coefficients):

b.kikuyu.pointhope          7 [1,] -25.814700 [2,] -79.276511 [3,]   7.517114 [4,] -30.409726 [5,]  59.803203 [6,]  90.246264 

my questions follows: need entire code repeat 10x (from initial 1000x loop output), i'm not sure how fit ungainly code regular loop or rep() command. need output of each of 10 repeats go empty dataframe.

i tried tacking @ end of original code repeated, without [,i] rewrote kikuyu.pointhope.df every time (manually) repeated it, , [,i] recieved error.

kikuyu.pointhope.df[,i] <- do.call(rbind, as.list(b.kikuyu.pointhope)) 

if possible, i'd output (which listed in column) inserted row in new dataframe, shown in initial data above. i've tried several iterations of cbind , rbind little effect.

thank time, sorry long question. i'm sure relatively basic, length of existing code tripping me up.

posting comment answer, op seems satisfied :-)

if you're asking how repeat bunch of stuff, easiest way bundle posted "wrapper" function, sapply(1:10,function(j) my_wrapper(your_arguments, subscripted j_th group of data) )


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -