【问题标题】:Creating subset matrix from bigger matrix in R从R中的更大矩阵创建子集矩阵
【发布时间】:2017-09-11 01:11:30
【问题描述】:

我有一个大矩阵(即 303*32)。我想创建一些列并在 R 中创建一个新矩阵。我找不到任何解决方案。任何帮助将不胜感激。

ads # Matrix of 303*32
new_mat <- matrix(c("speed","gaps","time")) # speed, gaps and times are names of cols which I am trying to exclude.

通过使用此代码,我只能获取列和行名称。

【问题讨论】:

    标签: r matrix subset


    【解决方案1】:

    如果您想按名称排除列,您需要类似:

    new_mat <- ads[, !colnames(ads) %in% c("speed","gaps","time")]
    

    【讨论】:

    • 我一直偏爱ads[, -match(c("speed","gaps","time"), colnames(ads))],因为如果你要求一个不存在的变量,它会出错。
    • @thelatemail 我认为如果您有同名的列,这将不起作用。我知道这很糟糕,但它可能会发生。
    猜你喜欢
    • 1970-01-01
    • 2015-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多