[R] How to loop through all the columns in dataframe

jim holtman jholtman at gmail.com
Sun Mar 16 02:29:10 CET 2008


Is this what you were looking for:

> xd<-
+ c(2.2024,2.4216,1.4672,1.4817,1.4957,1.4431,1.5676)
> pd<-
+ c(0.017046,0.018504,0.012157,0.012253,0.012348,0.011997,0.012825)
> td<- 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.70460e-02 1.85040e-02 1.21570e-02 1.22530e-02 1.2348e-02
1.1997e-02 1.2825e-02
td 1.60524e+05 1.63565e+05 1.43973e+05 1.11956e+05 8.9677e+04
9.5269e+04 8.1558e+04
>  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){
+     t(varA + trans[, .col[1]] * covAB + trans[, .col + 1] * covAB +
trans[, .col[1]] * trans[, .col + 1] * varB)
+ })
> # rbind for the output
> do.call(rbind, x)
                xd           pd        td
 [1,] 3.827555e-07 3.562187e-06 13817.642
 [2,] 5.216407e-07 3.570411e-06 12162.524
 [3,] 5.195306e-07 3.570286e-06  9457.751
 [4,] 5.174933e-07 3.570163e-06  7575.638
 [5,] 5.251477e-07 3.570618e-06  8048.046
 [6,] 5.070304e-07 3.569545e-06  6889.751
 [7,] 4.049294e-07 3.568518e-06 12392.937
 [8,] 4.044920e-07 3.568394e-06  9636.924
 [9,] 4.040698e-07 3.568271e-06  7719.154
[10,] 4.056562e-07 3.568725e-06  8200.512
[11,] 4.019013e-07 3.567653e-06  7020.273
[12,] 9.053716e-07 3.576633e-06  8482.585
[13,] 8.979174e-07 3.576510e-06  6794.531
[14,] 9.259239e-07 3.576965e-06  7218.231
[15,] 8.596348e-07 3.575891e-06  6179.364
[16,] 8.904145e-07 3.576385e-06  5283.524
[17,] 9.180195e-07 3.576841e-06  5612.999
[18,] 8.526806e-07 3.575766e-06  4805.162
[19,] 9.103878e-07 3.576717e-06  4495.999
[20,] 8.459661e-07 3.575643e-06  3848.924
[21,] 8.711933e-07 3.576098e-06  4088.938
>
>
>



On 3/15/08, Felipe Carrillo <mazatlanmexico at yahoo.com> wrote:
> Hi:
> Can anyone advice me on how to loop and perform a
> calculation through all the columns.
> here's my data
> xd<-
> c(2.2024,2.4216,1.4672,1.4817,1.4957,1.4431,1.5676)
> pd<-
> c(0.017046,0.018504,0.012157,0.012253,0.012348,0.011997,0.012825)
> td<- c(160524,163565,143973,111956,89677,95269,81558)
>  mydf<-data.frame(xd,pd,td)
>  trans<-t(mydf)
>  trans
>  I have these values that I need to include while
> looping:
>  varA<- 0.0000036084
>  covAB<- (-0.0000013046)
>  varB<- 0.00000052628
>  After transposing my dataframe I need something like
> the following:
>  varA + col1*covAB + col2*covAB + col1*col2*varB
>  varA + col1*covAB + col3*covAB + col1*col3*varB
>  varA + col1*covAB + col4*covAB + col1*col4*varB
>  varA + col1*covAB + col5*covAB + col1*col5*varB
>     and so on..
>     then do it all over again but starting with col2
>   varA + col2*covAB + col3*covAB + col2*col3*varB
>   varA + col2*covAB + col4*covAB + col2*col4*varB
>  varA + col2*covAB + col5*covAB + col2*col5*varB
>  then col3
>  then col4 to col7
>  This is easy to do in Excel but I wonder if there's
> a way to do it with R
>  This is the dataframe I get while doing a loop with
> Excel(the values generated could bedifferent).Thanks
>  xd               pd            td
> 3.82755E-07     26256108060     0.000315419
> 5.21641E-07     23111121852     0.000207228
> 5.19531E-07     17971624944     0.000208865
> 5.17464E-07     14395310748     0.000210484
> 5.25148E-07     15292960956     0.000204501
> 5.0703E-07      13092016392     0.000218615
> .       .       .
> 4.04929E-07     23548943745     0.000224953
> 4.04492E-07     18312083140     0.00022673
> 4.04064E-07     14668018505     0.000228487
> 4.05656E-07     15582673985     0.000221992
> 4.01901E-07     13340034270     0.000237314
> .       .       .
> 9.05372E-07     16118641188     0.00014896
> 8.97811E-07     12911066721     0.000150115
> 9.25924E-07     13716163737     0.000145848
> 8.59635E-07     11742149934     0.000155914
> .       .       .
> 8.9031E-07      10039878212     0.0001513
> 9.1802E-07      10665936164     0.000146999
> 8.52681E-07     9130907448      0.000157145
> .       .       .
> 9.10279E-07     8543438113      0.000148139
> 8.4587E-07      7313876766      0.000158363
> .       .       .
> 8.71193E-07     7769949102      0.000153862
>
>
>
>  Felipe D. Carrillo
>  Fishery Biologist
>  Department of the Interior
>  US Fish & Wildlife Service
>  California, USA
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>


-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list