Hi everyone, I am having trouble understanding where I went wrong with my code. It seems to logically be "all there" but the output says otherwise. I know this is a bit long but I can't seem to find the errors so I would appreciate your help :) This is my program : files<-Sys.glob("*.rescount.txt");length<-length(files);* #selecting all files of a particular extension, saving in a list* a<-1; while(a<=length) *#going through every element of the list* { df1<-read.table(files[a]); c.leng<-length(files[,1]); r.leng<-length(files[1,]); *#creating data frame for output with the same dimensions as input* opdf<-data.frame(matrix(rep(NA,nrow(df1)*ncol(df1)),nrow=nrow(df1))); opdf[,1]<-df1[,1]; opdf[1,]<-df1[1,]; *#copying the first row and first column so they have the same headers* b<-2; while(b<=c.leng) *#working through each row of the input data frame* { c<-2; while(c<=r.leng) *#working through each row element of a particular column* { n<-(df1[c][b,]); k<-1; while(k<=(n-1)) *#inner loop to go through a value of 'k' variable* { ... *#working with the code to generate a value* opdf[c][b,]<-sum; *#[1]* k<-k+1; } c<-c+1; } b<-b+1; } fname<-strsplit(files[a],".seq.ptseq.rescount.txt"); *#generating uniqe file names based on the input file names* ext<-".zsc.txt"; filename<-paste0(fname,ext); write.table(opdf,file=filename,row.names=FALSE,col.names=FALSE,quote=FALSE,sep="\t"); a<-a+1; } If the input data frame is supposed to be : *NAME V1 V2 V3* *V1' * 10 12 45 *V2' * 56 34 79 *V3' * 34 67 87 The output data frame should be : *NAME V1 V2 V3* *V1' * x y z *V2' * a b c *V3' * n p q (all the letters of the alphabet are various numbers generated by the program and filled in in the line marked by #[1] However my output file contains this: "x" "(name of input file)" [[alternative HTML version deleted]]
On 2013-03-12 05:29, Sahana Srinivasan wrote:> Hi everyone, I am having trouble understanding where I went wrong with my > code. It seems to logically be "all there" but the output says otherwise. > I know this is a bit long but I can't seem to find the errors so I would > appreciate your help :) > > This is my program : > > files<-Sys.glob("*.rescount.txt");length<-length(files);* #selecting all > files of a particular extension, saving in a list*Your use of the name 'length' is a bad idea (but not your problem); see fortune("dog"). If you think that your object 'files' is a 'list', you're mistaken; try str(files) to see that it's a character vector.> a<-1; > while(a<=length) *#going through every element of the list* > { > df1<-read.table(files[a]);I would _always_ look at the result of any file import with str() to ensure that the reading went as expected. Do (some of) your files have variable variable name headers?> c.leng<-length(files[,1]);I don't see why this instruction would not throw an error re "incorrect number of dimensions". Okay, at this point I gave up. Your code is not reproducible and you're posting in HTML. Please give at least a cursory look at the Posting Guide. [... rest of code sample snipped ...] Peter Ehlers
Your code appears to be a load of dingos' kidneys, but in general when troubleshooting one can do worse than be guided by fortune("magnitude and direction"). cheers, Rolf Turner On 03/13/2013 01:29 AM, Sahana Srinivasan wrote:> Hi everyone, I am having trouble understanding where I went wrong with my > code. It seems to logically be "all there" but the output says otherwise. > I know this is a bit long but I can't seem to find the errors so I would > appreciate your help :) > > This is my program : > > files<-Sys.glob("*.rescount.txt");length<-length(files);* #selecting all > files of a particular extension, saving in a list* > a<-1; > while(a<=length) *#going through every element of the list* > { > df1<-read.table(files[a]); > c.leng<-length(files[,1]); > r.leng<-length(files[1,]); *#creating data frame for output with the same > dimensions as input* > opdf<-data.frame(matrix(rep(NA,nrow(df1)*ncol(df1)),nrow=nrow(df1))); > opdf[,1]<-df1[,1]; > opdf[1,]<-df1[1,]; *#copying the first row and first column so they have > the same headers* > b<-2; > while(b<=c.leng) *#working through each row of the input data frame* > { > c<-2; > while(c<=r.leng) *#working through each row element of a particular > column* > { > n<-(df1[c][b,]); > k<-1; > while(k<=(n-1)) *#inner loop to go through a value of 'k' variable* > { > ... *#working with the code to generate a value* > opdf[c][b,]<-sum; *#[1]* > k<-k+1; > } > c<-c+1; > } > b<-b+1; > } > fname<-strsplit(files[a],".seq.ptseq.rescount.txt"); *#generating uniqe > file names based on the input file names* > ext<-".zsc.txt"; > filename<-paste0(fname,ext); > > write.table(opdf,file=filename,row.names=FALSE,col.names=FALSE,quote=FALSE,sep="\t"); > a<-a+1; > > } > > If the input data frame is supposed to be : > > *NAME V1 V2 V3* > *V1' * 10 12 45 > *V2' * 56 34 79 > *V3' * 34 67 87 > > The output data frame should be : > *NAME V1 V2 V3* > *V1' * x y z > *V2' * a b c > *V3' * n p q > (all the letters of the alphabet are various numbers generated by the > program and filled in in the line marked by #[1] > > However my output file contains this: > "x"