R 显示模型矩阵中缺少的级别
我想知道是否有办法在矩阵中插入一列,以便R 显示模型矩阵中缺少的级别,r,R,我想知道是否有办法在矩阵中插入一列,以便 p1 <- c("a","b","c","e","d","a","c") p2 <- c("a","b","c","e","e","a","c") p1mat <- model.matrix(~p1 + 0) p2mat <- model.matrix(~p2 + 0) colnames(p1mat) <- gsub("p1","",colnames(p1mat)) colnames(p2mat) <- gsub("
p1 <- c("a","b","c","e","d","a","c")
p2 <- c("a","b","c","e","e","a","c")
p1mat <- model.matrix(~p1 + 0)
p2mat <- model.matrix(~p2 + 0)
colnames(p1mat) <- gsub("p1","",colnames(p1mat))
colnames(p2mat) <- gsub("p2","",colnames(p2mat))
和p2mat
我的问题是,有没有一种方法可以将只包含零的列向量d偷偷带入矩阵p2mat?以致
d
0
0
0
0
0
0
0
向量被自动排序并放置在c列和e列之间,从而得到p2mat的以下矩阵
基本上,我希望矩阵p2mat查看p1mat中的每一列,以创建相同大小的矩阵,并通过虚拟矩阵跟踪数据
谢谢。您可以考虑两种输入,确保它们具有相同的级别。那么model.matrix应该按照您的预期工作
例如:
p1 <- c("a","b","c","e","d","a","c")
p2 <- c("a","b","c","e","e","a","c")
levs <- sort(unique(c(p1, p2)))
f1 <- factor(p1, levs)
f2 <- factor(p2, levs)
model.matrix(~f1 + 0)
# f1a f1b f1c f1d f1e
# 1 1 0 0 0 0
# 2 0 1 0 0 0
# 3 0 0 1 0 0
# 4 0 0 0 0 1
# 5 0 0 0 1 0
# 6 1 0 0 0 0
# 7 0 0 1 0 0
# attr(,"assign")
# [1] 1 1 1 1 1
# attr(,"contrasts")
# attr(,"contrasts")$f1
# [1] "contr.treatment"
model.matrix(~f2 + 0)
# f2a f2b f2c f2d f2e
# 1 1 0 0 0 0
# 2 0 1 0 0 0
# 3 0 0 1 0 0
# 4 0 0 0 0 1
# 5 0 0 0 0 1
# 6 1 0 0 0 0
# 7 0 0 1 0 0
# attr(,"assign")
# [1] 1 1 1 1 1
# attr(,"contrasts")
# attr(,"contrasts")$f2
# [1] "contr.treatment"
请尝试以下功能:
myfun(p1mat, p2mat)
myfun(p2mat, p1mat)
myfun(p3mat, p1mat)
myfun(p3mat, p1mat, p2mat)
您可以考虑两种输入,确保它们具有相同的级别。那么model.matrix应该按照您的预期工作
例如:
p1 <- c("a","b","c","e","d","a","c")
p2 <- c("a","b","c","e","e","a","c")
levs <- sort(unique(c(p1, p2)))
f1 <- factor(p1, levs)
f2 <- factor(p2, levs)
model.matrix(~f1 + 0)
# f1a f1b f1c f1d f1e
# 1 1 0 0 0 0
# 2 0 1 0 0 0
# 3 0 0 1 0 0
# 4 0 0 0 0 1
# 5 0 0 0 1 0
# 6 1 0 0 0 0
# 7 0 0 1 0 0
# attr(,"assign")
# [1] 1 1 1 1 1
# attr(,"contrasts")
# attr(,"contrasts")$f1
# [1] "contr.treatment"
model.matrix(~f2 + 0)
# f2a f2b f2c f2d f2e
# 1 1 0 0 0 0
# 2 0 1 0 0 0
# 3 0 0 1 0 0
# 4 0 0 0 0 1
# 5 0 0 0 0 1
# 6 1 0 0 0 0
# 7 0 0 1 0 0
# attr(,"assign")
# [1] 1 1 1 1 1
# attr(,"contrasts")
# attr(,"contrasts")$f2
# [1] "contr.treatment"
请尝试以下功能:
myfun(p1mat, p2mat)
myfun(p2mat, p1mat)
myfun(p3mat, p1mat)
myfun(p3mat, p1mat, p2mat)
此函数获取2个矩阵,并比较它们的维数。如果它们的尺寸不同,它会在缺少的精确列位置向矩阵中插入一列新的零,列数较少。因此,它产生了一个与另一个矩阵维数相同的新矩阵
match_matrices <- function(matrix1, matrix2) {
if(ncol(matrix1) != ncol(matrix2)) {
get_cols <- function(x) { l <- list(); for(i in 1:ncol(x)) { l[i] <- list(as.numeric(x[,i])) }; return(l) }
k <- get_cols(matrix2)
odd_one_out <- setdiff(colnames(matrix1), colnames(matrix2))
insert_at <- which(colnames(matrix1) == odd_one_out)
res <- t(do.call('rbind', append(k, list(rep(0, nrow(matrix2))), insert_at-1)))
colnames(res) <- colnames(matrix1)
}
return(res)
}
此函数获取2个矩阵,并比较它们的维数。如果它们的尺寸不同,它会在缺少的精确列位置向矩阵中插入一列新的零,列数较少。因此,它产生了一个与另一个矩阵维数相同的新矩阵
match_matrices <- function(matrix1, matrix2) {
if(ncol(matrix1) != ncol(matrix2)) {
get_cols <- function(x) { l <- list(); for(i in 1:ncol(x)) { l[i] <- list(as.numeric(x[,i])) }; return(l) }
k <- get_cols(matrix2)
odd_one_out <- setdiff(colnames(matrix1), colnames(matrix2))
insert_at <- which(colnames(matrix1) == odd_one_out)
res <- t(do.call('rbind', append(k, list(rep(0, nrow(matrix2))), insert_at-1)))
colnames(res) <- colnames(matrix1)
}
return(res)
}
啊,谢谢你!这正是我想要的。啊,谢谢!这正是我想要的。如果这是你将要采取的方法,你最好实际匹配每个矩阵的名称。从p1开始,如果这是你将要采取的方法,实际上,最好是匹配每个矩阵的名称。从p1开始,您是否只在一个方向上检查列的存在?或者您希望确保两个矩阵具有相同的列集合。如果是后者,您可能需要检查不同测试用例的match_matrix函数。您是否只检查一个方向上是否存在列?或者您希望确保两个矩阵具有相同的列集合。如果是后者,您可能需要查看不同测试用例的match_matrix函数。
myfun(p1mat, p2mat)
myfun(p2mat, p1mat)
myfun(p3mat, p1mat)
myfun(p3mat, p1mat, p2mat)
match_matrices <- function(matrix1, matrix2) {
if(ncol(matrix1) != ncol(matrix2)) {
get_cols <- function(x) { l <- list(); for(i in 1:ncol(x)) { l[i] <- list(as.numeric(x[,i])) }; return(l) }
k <- get_cols(matrix2)
odd_one_out <- setdiff(colnames(matrix1), colnames(matrix2))
insert_at <- which(colnames(matrix1) == odd_one_out)
res <- t(do.call('rbind', append(k, list(rep(0, nrow(matrix2))), insert_at-1)))
colnames(res) <- colnames(matrix1)
}
return(res)
}
match_matrices(p1mat, p2mat)