我创建了一个3D数组,并希望用其他两个data.frames
填充数据那些data.frames具有不同的colnames和rownames,因此当我处理不存在的单元格时,有时会弹出NULL。两个data.frames在其单元格中都有一个'lm'输出列表。
但问题是我一直收到这个错误:
Error in diff_models[i, j, "cont"] <- cont :
incorrect number of subscripts
我也注意到在创建时“diff_models”是一个逻辑类型(也很奇怪,顺便说一句),但是当错误弹出时它变成了一个列表。所以我猜问题是关于列表中没有[i,j,'cont']。但为什么循环会改变“diff_models”的类型?
cont_col <- colnames(temp1)
cont_row <- rownames(temp1)
dis_col <- colnames(temp2)
dis_row <- rownames(temp2)
cols <- unique(c(cont_col,dis_col))
rows <- unique(c(cont_row,dis_row))
diff_models <- array(NA, c(length(rows),length(cols),2), dimnames =
list('predictor'=rows,'response'=cols, 'condition'=c('dis','cont')))
for (j in cols) {
for (i in rows) {
cont <- cont_models[i,j]
dis <- dis_models[i,j]
diff_models[i,j,"dis"] <- ifelse(is.null(dis),NA,dis)
diff_models[i,j,"cont"] <- ifelse(is.null(cont),NA,cont)
}
}
使用
diff_models[i][j]["dis"] <- ifelse(is.null(dis),NA,dis)
diff_models[i][j]["cont"] <- ifelse(is.null(cont),NA,cont)
不会以错误结束,而是将“diff_models”变为空列表。
将数字保存到数组中,但是效果非常好