在未指定特定列名的情况下从 R 中的矩阵子集列

Subsetting Column from Matrix in R without specific column names specified

我用字符名称“0”,...,“10”制作了一个矩阵a。现在,我创建了一个列名 S 的子集列表。我想对矩阵 a 进行子集化,这样我就不会有 S 中带有名称的列。我正在尝试执行以下操作,但它给出了错误。有什么想法吗?

 > a
          0  1  2  3  4  5  6  7  8  9  10
     [1,] 1  1 11 21 31 41 51 61 71 81  91
     [2,] 1  2 12 22 32 42 52 62 72 82  92
     [3,] 1  3 13 23 33 43 53 63 73 83  93
     [4,] 1  4 14 24 34 44 54 64 74 84  94
     [5,] 1  5 15 25 35 45 55 65 75 85  95
     [6,] 1  6 16 26 36 46 56 66 76 86  96
     [7,] 1  7 17 27 37 47 57 67 77 87  97
     [8,] 1  8 18 28 38 48 58 68 78 88  98
     [9,] 1  9 19 29 39 49 59 69 79 89  99
    [10,] 1 10 20 30 40 50 60 70 80 90 100
    > colnames(a)
     [1] "0"  "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10"

> S <- as.character(c(0,2))
> S
[1] "0" "2"
> a[,-S]
Error in -S : invalid argument to unary operator

你可以试试

a[,setdiff(colnames(a), S)]

或者

a[,!colnames(a) %in% S]