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

Felipe Carrillo mazatlanmexico at yahoo.com
Sun Mar 16 01:17:09 CET 2008


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



More information about the R-help mailing list