r - Ordering Merged data frames -


as new r programmer seem have run strange problem - inexperience r

after reading , merging successive files single data frame, find order not sort data expected.

i have multiple references in each file each file refers measurement data obtained @ different time.

here's code

library(reshape) # enter file name read & save data filename=readline("enter file name:\n") # find first occurance of file ( 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) ( 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 between lab results ( in 1 : nsmpls) { if (res[i,"bw"] > 3 | res[i,"bw"] < -3) res[i,"pf_zbw"]=1 } # evaluate z_scores within lab results ( in 1 : nsmpls) { if (res[i,"wi"] > 3 | res[i,"wi"] < -3) res[i,"pf_zwi"]=1 }  dd = melt(res, id=c("lab","r"), "pf_zbw") b = cast(dd, lab ~ r) 

if see why ordering works 55 of 70 records , steer me in right direction obliged

thanks much

check whether z$lab_id factor (with is.factor(z$lab_id)).

if is, try

z$lab_id <- as.character(z$lab_id) 

if supposed character vector; or

z$lab_id <-    as.numeric(as.character(z$lab_id)) 

if supposed numeric vector.

then order again.

ps. had put these in comments.


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -