0

I have a data frame 'QARef" whith 25 variables. There are only 5 unique jobs (3rd column) but lots of rows per job:

str(QARef) 'data.frame': 648 obs. of 25 variables:

I'm using tapply to generate mean values across all 5 jobs for certain rows:

RefMean <- tapply(QARef$MTN,
                  list(QARef$Target_CD, QARef$Feature_Type, QARef$Orientation, QARef$Contrast, QARef$Prox),
                  FUN=mean, trim=0, na.rm=TRUE)

and I get something I'm hoping is referred to as multidimensional list:

str(RefMean)
 num [1:17, 1:2, 1:2, 1:2, 1:2] 34.1 34.2 25.2 28.9 29.2 ...
 - attr(*, "dimnames")=List of 5
  ..$ : chr [1:17] "55" "60" "70" "80" ...
  ..$ : chr [1:2] "LINE" "SQUARE"
  ..$ : chr [1:2] "X" "Y"
  ..$ : chr [1:2] "CLEAR" "DARK"
  ..$ : chr [1:2] "1:1" "Iso"

What I want to do is add a column to QARef which contains the correct RefMean value for each row depending on a match between values in columns of QARef and dimnames of RefMean. E.g. QARef column Feature_Type=="LINE" should match the dimname "LINE" etc.

Any hint how to do this or where to find the answer would be highly appreciated.

aynber
  • 22,380
  • 8
  • 50
  • 63
MarkH
  • 122
  • 9
  • Is this `R`? if so could you edit your tags – EdChum May 26 '15 at 12:03
  • perhaps `aggregate` will help... it's hard to tell without a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – shadow May 28 '15 at 13:39

1 Answers1

0

I think I found solution. Probably not elegant but it works:

RefMean <- data.frame(tapply(QARef$MTN,paste(QARef$Target_CD,QARef$Feature_Type,QARef$Orientation,QARef$Contrast,QARef$Prox,QARef$Measurement_Type),FUN=mean,trim=0,na.rm=TRUE))
colnames(RefMean) <- c("MTN_Ref")
Ident <- do.call(rbind, strsplit(rownames(RefMean), " "))
RefMean["Target_CD"] <- Ident[,1]
RefMean["Feature_Type"] <- Ident[,2]
RefMean["Orientation"] <- Ident[,3]
RefMean["Contrast"] <- Ident[,4]
RefMean["Prox"] <- Ident[,5]
RefMean["Measurement_Type"] <- Ident[,6]
QA4 <- merge(QARef,RefMean,by=c("Target_CD","Feature_Type","Orientation","Contrast","Prox","Measurement_Type"),all.x=TRUE,sort=FALSE)
MarkH
  • 122
  • 9