title: Ordering Merged data frames
body: <p>As a fairly new R programmer I seem to have run into a strange problem - probably my inexperience with R</p>
  
  <p>After reading and merging successive files into a single data frame, I find that order does not sort the data as expected. </p>
  
  <p>I have multiple references in each file but each file refers to measurement data obtained at a different time.</p>
  
  <p>Here's the code</p>
  
  <pre><code>library(reshape)
  # Enter file name to Read &amp; Save data
  FileName=readline("Enter File name:\n")
  # Find first occurance of file
  for ( round1 in 1 : 6) {
  ReadFile=paste(round1,"C_",FileName,"_Stats.csv", sep="")
  if (file.exists(ReadFile))
  break
  }
  
  x = data.frame(read.csv(ReadFile, header=TRUE),rnd=round1)
  for ( round2 in (round1+1) : 6) {
  #
  ReadFile=paste(round2,"C_",FileName,"_Stats.csv", sep="")
  if (file.exists(ReadFile)) {
  y = data.frame(read.csv(ReadFile, header=TRUE),rnd = round2)
      if (round2 == (round1 +1))
      z=data.frame(merge(x,y,all=TRUE))
      z=data.frame(merge(y,z,all=TRUE))
  }
  }
  ordered = order(z$lab_id)
  
  results = z[ordered,]
  
  res = data.frame( lab=results[,"lab_id"],bw=results[,"ZBW"],wi=results[,"ZWI"],pf_zbw=0,pf_zwi=0,r = results[,"rnd"])
  
  
  #
  # Establish no of samples recorded
  nsmpls = length(res[,c("lab")])
  
  # Evaluate Z_scores for Between Lab Results
  for ( i in 1 : nsmpls) {
  if (res[i,"bw"] &gt; 3 | res[i,"bw"] &lt; -3)
  res[i,"pf_zbw"]=1
  }
  # Evaluate Z_scores for Within Lab Results
  for ( i in 1 : nsmpls) {
  if (res[i,"wi"] &gt; 3 | res[i,"wi"] &lt; -3)
  res[i,"pf_zwi"]=1
  }
  
  dd = melt(res, id=c("lab","r"), "pf_zbw")
  b = cast(dd, lab ~ r)
  </code></pre>
  
  <p>If anyone could see why the ordering only works for about 55 of 70 records and could steer me in the right direction I would be obliged</p>
  
  <p>Thanks very much</p>