0

I made a matrix a with character names "0", ..., "10". Now I make a subset list of column names, S. I want to subset the matrix a so that, I won't have the columns with names in S. I am trying to do the following but it's giving error. Any idea?

 > 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
1
  • 1
    Try a[,setdiff(colnames(a), S)] or a[,!colnames(a) %in% S] Commented Feb 11, 2015 at 18:17

1 Answer 1

1

You can try

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

Or

a[,!colnames(a) %in% S]
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.