[R] Subsetting matrices

Tyler Smith tyler.smith at mail.mcgill.ca
Tue Feb 7 23:37:50 CET 2006


Dear R-Helpers,

I am working on a dataset containing ca. 500 specimens of 17 different 
species. The data is three text columns, followed by ca 70 columns of 
binary data.

I'm trying to write a function that will allow me to interactively 
exclude species using the inofrmation in the third text column. My 
function, matrix.slicer, appears to work as I intend. However, when I 
pass the resulting matrix to my homemade principal coordinates 
function, pcoa, the results include all of the data, with nothing 
excluded.

I've read the help for factors, levels, subset, [, and some threads 
here relating to subsetting and drop=true issues, but I'm afraid I just 
don't understand what I need to do. I've pasted the functions below - 
any help would be appreciated. Thanks!

Tyler Smith


matrix.slicer <- function(mat, label.vector){

  selector <- 999

  print("Select species to remove from the matrix, 0 to finish")
   while (selector != 0){
    selector <-  menu( levels( factor(mat[,label.vector])))
    if (selector==0)break
    print (c("Removed: ", levels(factor(mat[,label.vector]))[selector]))
    mat <- subset (mat, mat[,label.vector]!=levels (factor (mat 
[,label.vector]))[selector])
         }

  return (mat)
  }


gel.data <- matrix.slicer (gel.data,3)

disttemp <- as.matrix(vegdist(gel.data[,-(1:3)], method="jaccard"))

dist <- sqrt(disttemp)

PCOA <- pcoa (dist)


-- 
Tyler Smith




More information about the R-help mailing list