Skip to content
Prev 206570 / 398503 Next

Help using Cast (Text) Version

Bingo,

I knew it was something simple and that I wasn't seeing the wood for the 
trees.

David, Ista

Apologies for the vague description, which I thought was clear enough, but 
yes I now think that I understand that I need to count the 1's and be able 
to sum the total of 1' and 0's by ignoring the NA's, which as David you have 
correctly identified is in res. Of course as you have quite correctly said 
by the time it's melt-cast there is now way to distinguish between NA's and 
0's.

Here is the original code so that you can see where res comes from; Ista I 
hope that this is now clearer for you.

 library(reshape)
# Enter file name to Read & Save data
FileName=readline("Enter File name:\n")
SampleName=readline("Enter Sample (A,B or C):\n")
#for (sname in 1 : 3) {
#if (sname == 1)
#  SampleName =  "A"
#  if (sname == 2)
#    SampleName =  "B"
#    if (sname == 3)
#      SampleName =  "C"
#for ( fname in 1 : 4) {
#if (fname == 1)
#  FileName = "SPC"
#  if (fname == 2)
#    FileName = "Coli"
#      if (fname == 3)
#      FileName = "Colif"
#       if (fname == 4)
#          FileName = "Ecoli"

# Find first occurance of file
for ( rloop1 in 1 : 6) {
ReadFile=paste(rloop1,SampleName,"_",FileName,"_Stats.csv", sep="")
if (file.exists(ReadFile))
break
}
x = data.frame(read.csv(ReadFile, header=T),rnd=rloop1)
for ( rloop2 in (rloop1+1) : 6) {
ReadFile=paste(rloop2,SampleName,"_",FileName,"_Stats.csv", sep="")
if (file.exists(ReadFile)) {
    y = data.frame(read.csv(ReadFile, header=T),rnd = rloop2)
    if (rloop2 == (rloop1+1))
       z=merge(x,y,all=T)
       z=merge(y,z,all=T)
## The next piece of code is where there are not two successive rounds of 
data.
## It must be modified for each year's summary
##
##if ( (FileName == "Coli") & (SampleName == "B")) {
##    if (rloop2 == (rloop1+3))
##        z=merge(x,y,all=T)
##        z=merge(y,z,all=T)
##        }
##
##
   }
}

results <- z
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"] > 3 | res[i,"bw"] < -3)
res[i,"pf_zbw"]=1
}
#Evaluate Z_scores for Within Lab Results
for ( i in 1 : nsmpls) {
if (res[i,"wi"] > 3 | res[i,"wi"] < -3)
res[i,"pf_zwi"]=1
}
# Melt and Cast the 'res' frame and then order it
bw = melt(res, id=c("lab","r"), "pf_zbw")
# b = cast(bw, lab ~ r)
# bw_eval = b[order(as.character(b$lab)),]

##### Code for summing the no of Fails for Between Results
#bsum = cast(bw, lab ~ r, margins=TRUE, sum)


## Save Summary of Between Results
## FileSaveBw=paste(SampleName,"_",FileName,"_2009Between.csv",sep="")
## write.csv(bw_eval,file=FileSaveBw)
##
##
##
####
# Melt and Cast the 'res' frame and then order it
wi = melt(res, id=c("lab","r"), "pf_zwi")
w = cast(wi, lab ~ r)
wi_eval = w[order(as.character(w$lab)),]

##### Code for summing the no of Fails for Within Results
#wsum = cast(wi, lab ~ r, margins=TRUE, sum)
##
## Save Summary of Within Results
## FileSaveWi=paste(SampleName,"_",FileName,"_2009Within.csv",sep="")
## write.csv(wi_eval,file=FileSaveWi)
###### cat ("File Name: ",FileName,"Sample Name: ",SampleName, "\n")

#  }

#}
end

Once again thanks for your interest
Steve

----- Original Message ----- 
From: "David Winsemius" <dwinsemius at comcast.net>
To: "Steve Sidney" <sbsidney at mweb.co.za>
Cc: <r-help at r-project.org>
Sent: Sunday, January 17, 2010 7:36 PM
Subject: Re: [R] Help using Cast (Text) Version