如何从R矩阵中按名称删除列?

如何从R矩阵中按名称删除列?,r,R,我知道如何从R dataframe中按名称删除列,但我不知道如何在matrix中这样做。我想这样做: > m <- matrix(1:6, nrow=2, ncol = 3) > colnames(m) <- c("q", "w", "e") > m q w e [1,] 1 3 5 [2,] 2 4 6 > variable_to_remove <- "q" > m[,variable_to_remove] <- NULL Err

我知道如何从R dataframe中按名称删除列,但我不知道如何在matrix中这样做。我想这样做:

> m <- matrix(1:6, nrow=2, ncol = 3)
> colnames(m) <- c("q", "w", "e")
> m
     q w e
[1,] 1 3 5
[2,] 2 4 6
> variable_to_remove <- "q"
> m[,variable_to_remove] <- NULL
Error in m[, variable_to_remove] <- NULL : 
number of items to replace is not a multiple of replacement length
我真的不喜欢使用get()、eval()、parse()等函数。有更简单更好的方法吗?

m[,colnames(m)!=“q”]
m[,!colnames(m)%in%c(“q”)]
> subset(m, select=-c(get(variable_to_remove)))
     w e
[1,] 3 5
[2,] 4 6