1

Suppose I have a matrix called mymat. I need to exclude columns key and AMLM12014N-R and make another matrix called newmat. I can simply do this like this: newmat <- mymat[,-c(1,4)], but is there a way to do it by calling column name itself? Something like this: newmat <- mymat[,-c("key","AMLM12014N-R")] ?

mymat <- structure(c("chr5:12111", "chr5:12111", "chr5:12113", "chr5:12114", 
"chr5:12118", "0N", "0N", "1N", "0N", "0N", "00", "00", "00", 
"11", "10", "00", "00", "1N", "0N", "00"), .Dim = c(5L, 4L), .Dimnames = list(
    c("34", "35", "36", "37", "38"), c("key", "AMLM12001KP", 
    "AMAS-11.3-Diagnostic", "AMLM12014N-R")))
MAPK
  • 5,635
  • 4
  • 37
  • 88
  • Searching the exact title of your post in Google gives you a number of answers from Stackoverflow. If you can think of a title, you can search using that title before posting another question. You've asked 28 questions in December alone! – thelatemail Dec 23 '15 at 03:04

1 Answers1

3

We can use setdiff

mymat[,setdiff(colnames(mymat), c("key","AMLM12014N-R"))]

Or %in%

mymat[,!colnames(mymat) %in% c("key","AMLM12014N-R")]
akrun
  • 874,273
  • 37
  • 540
  • 662