[R] How to loop through all the columns in dataframe
Felipe Carrillo
mazatlanmexico at yahoo.com
Mon Mar 17 04:23:53 CET 2008
--- jim holtman <jholtman at gmail.com> wrote:
> Glad I could help. You might want to post it back
> to R-Help so that
> others can see what was done.
>
> On Sun, Mar 16, 2008 at 6:02 PM, Felipe Carrillo
> <mazatlanmexico at yahoo.com> wrote:
> > Jim: I owe you man, this is great,I never thought
> that
> > I could acomplish this task. Now I can estimate
> > confidence intervals for mydf and I will be
> > done,,Thanks again Jim..
> >
> >
> > > This should do what you want: (you had td and pd
> > > reversed in your example)
> > >
> > > > xd <-
> > >
> c(2.2024,2.4216,1.4672,1.4817,1.4957,1.4431,1.5676)
> > > > td <-
> > >
> >
>
c(0.017046,0.018504,0.012157,0.012253,0.012348,0.011997,0.012825)
> > > > pd <-
> > > c(160524,163565,143973,111956,89677,95269,81558)
> > > > mydf<-data.frame(xd,pd,td)
> > > > trans<-t(mydf)
> > > > trans
> > > [,1] [,2] [,3]
> [,4]
> > > [,5]
> > > [,6] [,7]
> > > xd 2.20240e+00 2.42160e+00 1.46720e+00
> 1.48170e+00
> > > 1.4957e+00
> > > 1.4431e+00 1.5676e+00
> > > pd 1.60524e+05 1.63565e+05 1.43973e+05
> 1.11956e+05
> > > 8.9677e+04
> > > 9.5269e+04 8.1558e+04
> > > td 1.70460e-02 1.85040e-02 1.21570e-02
> 1.22530e-02
> > > 1.2348e-02
> > > 1.1997e-02 1.2825e-02
> > > > varA<- 0.0000036084
> > > > covAB<- (-0.0000013046)
> > > > varB<- 0.00000052628
> > > >
> > > > # create the sequences to test against
> > > > i.seq <- lapply(seq(ncol(trans) - 1),
> function(x)
> > > x:(ncol(trans) - 1))
> > > > x <- lapply(i.seq, function(.col){
> > > + # compute the 3 columns of data
> > > + cbind(xp=varA + trans[1, .col[1]] * covAB
> +
> > > trans[1, .col + 1] *
> > > covAB + trans[1, .col[1]] * trans[1, .col + 1] *
> > > varB,
> > > + pd=trans[2, .col[1]] * trans[2, .col +
> 1],
> > > + td=trans[3, .col[1]] * trans[3, .col +
> 1])
> > > + })
> > > > # rbind for the output
> > > > z <- do.call(rbind, x)
> > > > # add the covariance
> > > > z <- cbind(z, cov=z[, 'xp'] * z[, 'pd'] / z[,
> > > 'td'])
> > > > z
> >
> >
> >
> > Felipe D. Carrillo
> > Fishery Biologist
> > Department of the Interior
> > US Fish & Wildlife Service
> > California, USA
> >
> >
> >
> >
>
____________________________________________________________________________________
> > Be a better friend, newshound, and
>
> >
> >
>
>
>
> --
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
>
> What is the problem you are trying to solve?
>
Felipe D. Carrillo
Fishery Biologist
Department of the Interior
US Fish & Wildlife Service
California, USA
More information about the R-help
mailing list