0
    C<-c(1,3,4,5,5,5,6,4,6)         
    result<-which(C>5,arr.in=TRUE)

gives the index when condition is true.

It gives 7 and 9 to be true

I require that these index are store in matrix as 1 or 0. for example if I iterate this code 5 times by changing the values of C arbitrary then final result of matrix will be

    0 0 0 0 0 0 1 0 1
    1 0 0 1 0 0 0 1 0
    0 0 0 0 1 0 0 1 1
    1 1 1 0 0 0 1 1 0
    0 0 0 0 0 0 1 0 0

Please help

itfeature.com
  • 380
  • 4
  • 16

1 Answers1

2

If I understand correctly you would like to create a matrix of zeros or ones based upon the results of your which call. If so, ifelse() will probably be a better option as ifelse(C>5,0,1) returns the exact vector you would like so all you would need to do is combine all of those vectors together. You didn't supply your list of "C" vectors, so I wrote a quick function to generate some vectors to show you how this can work:

> #function to generate a "C" vector
> makeC <- function(x){
+   set.seed(x)
+   round(runif(10,0,10))
+ }
> 
> #create a list of "C" vectors
> c.list <- lapply(1:5,makeC)
> #look at list of your vectors that you want binary indices
> c.list
[[1]]
 [1] 3 4 6 9 2 9 9 7 6 1

[[2]]
 [1] 2 7 6 2 9 9 1 8 5 5

[[3]]
 [1] 2 8 4 3 6 6 1 3 6 6

[[4]]
 [1] 6 0 3 3 8 3 7 9 9 1

[[5]]
 [1]  2  7  9  3  1  7  5  8 10  1

> #make a list of your binary indices
> c.bin.list <- lapply(c.list,function(x) ifelse(x>5,1,0))
> #lookat your list of binary indices
> c.bin.list
[[1]]
 [1] 0 0 1 1 0 1 1 1 1 0

[[2]]
 [1] 0 1 1 0 1 1 0 1 0 0

[[3]]
 [1] 0 1 0 0 1 1 0 0 1 1

[[4]]
 [1] 1 0 0 0 1 0 1 1 1 0

[[5]]
 [1] 0 1 1 0 0 1 0 1 1 0

> #combine all of your binary indice vectors into a matrix with rbind()
> c.bin <- do.call(rbind,c.bin.list)
> #look at your matrix of binary indices
> c.bin
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    0    0    1    1    0    1    1    1    1     0
[2,]    0    1    1    0    1    1    0    1    0     0
[3,]    0    1    0    0    1    1    0    0    1     1
[4,]    1    0    0    0    1    0    1    1    1     0
[5,]    0    1    1    0    0    1    0    1    1     0
> #this can also be collapsed into a one-liner
> do.call(rbind,lapply(1:5, function(x) ifelse(makeC(x)>5,1,0)))
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    0    0    1    1    0    1    1    1    1     0
[2,]    0    1    1    0    1    1    0    1    0     0
[3,]    0    1    0    0    1    1    0    0    1     1
[4,]    1    0    0    0    1    0    1    1    1     0
[5,]    0    1    1    0    0    1    0    1    1     0
David
  • 9,284
  • 3
  • 41
  • 40
  • Thanks a lot dear. can you explain how this code work. I could not understand why function(x) is used, what does 1:10 mean and what is the purpose of do.call. thanks in advance – itfeature.com May 31 '13 at 17:01
  • Ok, I added a number of comments that are hopefully more clear. If there is still confusion over how do.call() or lapply() work then ther e are many other threads should be able to address that, like: http://stackoverflow.com/questions/3505701/r-grouping-functions-sapply-vs-lapply-vs-apply-vs-tapply-vs-by-vs-aggrega and http://stackoverflow.com/questions/10801750/whats-the-difference-between-lapply-and-do-call-in-r – David May 31 '13 at 17:11