简体   繁体   English

将data.frame与多维列表合并

[英]merge data.frame with multidimensional list

I have a data frame 'QARef" whith 25 variables. There are only 5 unique jobs (3rd column) but lots of rows per job: 我有一个带有25个变量的数据框“ QARef”。只有5个唯一的作业(第3列),但每个作业有很多行:

str(QARef) 'data.frame': 648 obs. str(QARef)'data.frame':648磅。 of 25 variables: 25个变量中:

I'm using tapply to generate mean values across all 5 jobs for certain rows: 我正在使用tapply生成某些行的所有5个作业的平均值:

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. 我想做的是向QARef添加一列,该列包含每行正确的RefMean值,具体取决于QARef列中的值与RefMean的暗名之间的匹配。 Eg QARef column Feature_Type=="LINE" should match the dimname "LINE" etc. 例如,QARef列Feature_Type ==“ LINE”应该匹配暗号“ LINE”等。

Any hint how to do this or where to find the answer would be highly appreciated. 任何提示如何做到这一点或在哪里找到答案将不胜感激。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM