2
votes

I have a matrix as like this

structure(c("cat", "Dog", "Dog1", "Cow", "E02.3", "E06.7", "E01.9", "E01.0", "Twe.03", "Twl.06", "Twl.15", "Twl.00", "CA/DO", "CA", "DO", "CA"), .Dim = c(4L, 4L), .Dimnames = list(NULL, c("Name", "Id1", "Id2", "Type")))

I need to split as two array, only the Id1 values based on the Type column value as like this

CA

   E02.3
   E06.7
   E01.0

DO

  E02.3
  E01.9

CA/DO values indicating the common names.

2

2 Answers

2
votes

Using grep.

cbind(m[grep("CA", m[,"Type"]), "Id1"])
#      [,1]   
# [1,] "E02.3"
# [2,] "E01.9"
cbind(m[grep("DO", m[,"Type"]), "Id1"])
#      [,1]   
# [1,] "E02.3"
# [2,] "E06.7"
# [3,] "E01.0"
1
votes

An option with strsplit

with(stack(setNames(strsplit(m1[, "Type"], "/", fixed = TRUE), 
      m1[, "Id1"])), split(as.character(ind), values))