Hi everyone, I'm using the following code to go over every element of a data frame (row wise). The problem I am facing is that the outer 'x' variable is not incrementing itself, thus, only one row of values is obtained, and the program does not proceed to the next row. This is the code: while(x<=coln) { while(y<=rown) { n<-as.numeric(df[[y]][x]); if(n>0) { lim<-(n-1); S<-100; (...) } opdf[[y]][x]<-sum; } y<-y+1; } x<-x+1; } Here is a sample of the input data: GENE A CD EF GH IK LM NP QR ST VW Y 2amt:Amet_0001 29 023 3417 1612 4229 39635 20 1325 3427 323 12 3amt:Amet_0002 19 315 4212 188 3525 437 2613 914 2120 30 0 8 [[alternative HTML version deleted]]
Hi, sorry that got sent without the output : Please ignore the aligning in the input, I am re-adding that here as well : 1GENEACDEFGHIKLMNPQRSTVWY2amt:Amet_00012902334171612422939635201325342732312 3amt:Amet_000219315421218835254372613914212030084 Output: 1GENEACDEFGHIKLMNPQRSTVWY2amt:Amet_00010.831808022254316NA0.831808022254316 0.8318080222543160.8318080222543160.8318080222543160.831808022254316 0.8318080222553840.8318080222553840.8318080222553840.831808022255384 0.8318080222553840.8318080222553840.8318080222553840.831808022255384 0.8318080222553840.8318080222553840.8318080222553840.831808022255384 0.8318080222553843amt:Amet_0002NANANANANANANANANANANANANANANANANANANANA4 On Mon, Mar 25, 2013 at 5:43 PM, Sahana Srinivasan < sahanasrinivasan.91@gmail.com> wrote:> Hi everyone, > I'm using the following code to go over every element of a data frame (row > wise). The problem I am facing is that the outer 'x' variable is not > incrementing itself, thus, only one row of values is obtained, and the > program does not proceed to the next row. > > This is the code: > while(x<=coln) > { > > while(y<=rown) > { > n<-as.numeric(df[[y]][x]); > if(n>0) > { > lim<-(n-1); > S<-100; > (...) > } > opdf[[y]][x]<-sum; > } > y<-y+1; > } > x<-x+1; > } > > Here is a sample of the input data: > > > GENE A CD EF GH IK LM NP QR ST VW Y 2amt:Amet_0001 29 023 3417 1612 422939 > 6 3520 1325 3427 323 12 3amt:Amet_0002 19 315 4212 188 3525 437 2613 91421 > 20 300 8 > > > >[[alternative HTML version deleted]]
On 25-03-2013, at 18:43, Sahana Srinivasan <sahanasrinivasan.91 at gmail.com> wrote:> Hi everyone, > I'm using the following code to go over every element of a data frame (row > wise). The problem I am facing is that the outer 'x' variable is not > incrementing itself, thus, only one row of values is obtained, and the > program does not proceed to the next row. > > This is the code: > while(x<=coln) > { > > while(y<=rown) > { > n<-as.numeric(df[[y]][x]); > if(n>0) > { > lim<-(n-1); > S<-100; > (...) > } > opdf[[y]][x]<-sum; > } > y<-y+1; > } > x<-x+1; > } >> Here is a sample of the input data: > > > GENE A CD EF GH IK LM NP QR ST VW Y 2amt:Amet_0001 29 023 3417 1612 4229 39635 > 20 1325 3427 323 12 3amt:Amet_0002 19 315 4212 188 3525 437 2613 914 2120 30 > 0 8This does not provide reproducible code or an example. You should also count the opening { and the closing }. Berend
Your immediate problem is that 'y' is not reset to 1. Easier code to write would be to use 'for' loops rather than 'while' loops. Better still would be to use neither if possible. I suspect that you are in Circle 3 of 'The R Inferno'. http://www.burns-stat.com/documents/books/the-r-inferno/ Pat On 25/03/2013 17:43, Sahana Srinivasan wrote:> Hi everyone, > I'm using the following code to go over every element of a data frame (row > wise). The problem I am facing is that the outer 'x' variable is not > incrementing itself, thus, only one row of values is obtained, and the > program does not proceed to the next row. > > This is the code: > while(x<=coln) > { > > while(y<=rown) > { > n<-as.numeric(df[[y]][x]); > if(n>0) > { > lim<-(n-1); > S<-100; > (...) > } > opdf[[y]][x]<-sum; > } > y<-y+1; > } > x<-x+1; > } > > Here is a sample of the input data: > > > GENE A CD EF GH IK LM NP QR ST VW Y 2amt:Amet_0001 29 023 3417 1612 4229 39635 > 20 1325 3427 323 12 3amt:Amet_0002 19 315 4212 188 3525 437 2613 914 2120 30 > 0 8 > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Patrick Burns pburns at pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming')