user2386786
user2386786

Reputation: 735

Write data from two for loops to a matrix

I have the following dataset:

data<-NULL
pA<-NULL
pB<-NULL
field<-c("A","A","A","A","B","B","B","B")
trt<-c("X","X","Y","Y")
patternA<-c("none","none","none","none","T","T","none",
"none","T","T","none","none","T","T","T","T")
patternB<-c("T","T","T","T","T","T","T","T","none","none","T","T","T","T","T","T")
df<-data.frame(field,trt,patternA,patternB)

now I tried to do something like this:

for (i in unique(df$field)){
for (j in unique(trt))
{ pA[j]<-nrow(df[df$patternA=="T"&df$trt==j,])/nrow(df[df$trt==j,])
pB[j]<-nrow(df[df$patternB=="T"&df$trt==j,])/nrow(df[df$trt==j,])
}
fd<-i
 p<-cbind(fd,pA,pB)
 data<-rbind(data,p)
 }

To get something like. This is what I would achieve by running the two loops of a subset of df by field (First by field "A", then by field "B")

data:

field pA  pB 
X "A"   "0.5" "0.5"
Y "A"   "0" "1"
X "B"   "1" "1"
Y "B"   "0.5" "1"

However, R gives me something like:

   fd  pA     pB    
X "A" "0.75" "0.75"
Y "A" "0.25" "1"   
X "B" "0.75" "0.75"
Y "B" "0.25" "1"  

which is not what I want. What did I do wrong? I would like to stick with the looping approach here if possible although I am aware of some of the downsides.

Upvotes: 0

Views: 73

Answers (1)

Henrik
Henrik

Reputation: 67778

You may also try ddply:

library(plyr)
ddply(df, .(trt, field), summarize,
  pA = sum(patternA == "T")/length(patternA),
  pB = sum(patternB == "T")/length(patternA))

#   trt field  pA  pB
# 1   X     A 0.5 0.5
# 2   X     B 1.0 1.0
# 3   Y     A 0.0 1.0
# 4   Y     B 0.5 1.0

Also note that components of a matrix should be of the same mode. Thus, your numeric proportions will be coerced to character when you put them in a matrix together with 'field', which is a character. Here I create a data frame, where each column can be of different mode.

Upvotes: 1

Related Questions