2
Q1=c(0,1,0,1,0,1,0,1)
Q2=c(1,0,0,0,1,1,1,0)
Q3=c(0,0,0,0,0,0,0,0)
Q4=c(1,0,0,0,1,1,1,0)
Q = cbind(Q1,Q2, Q3, Q4)
Q = matrix(Q, 8, 4)

     [,1] [,2] [,3] [,4]
[1,]    0    1    0    1
[2,]    1    0    0    0
[3,]    0    0    0    0
[4,]    1    0    0    0
[5,]    0    1    0    1
[6,]    1    1    0    1
[7,]    0    1    0    1
[8,]    1    0    0    0

I want to write a function

ifelse(Q[1]==1||Q[2]==1, 1,0)

and then keep increasing for column 3 and 4

ifelse(Q[3]==1||Q[4]==1, 1,0)

Return matrix

This is my code:

n = function(n){
x <- matrix(n row= 8,n col=n)
for(i in 1:n){
  for (j in 1: 4){
i = 1
j = 1 
x[,i]= apply(Q, 1, function(x)if else(x[j]==1||x[j+1]==1, 1,0))
j = j+2
}
return(x)
}
}
n(1)
n(2)

     [,1] [,2]
[1,]    1   NA
[2,]    1   NA
[3,]    0   NA
[4,]    1   NA
[5,]    1   NA
[6,]    1   NA
[7,]    1   NA

I think I did something wrong,the new matrix suppose, plus I have over 100 columns, so I have to write increase loop every 2 columns

      [,1] [,2]
[1,]    1   1
[2,]    1   0
[3,]    0   0
[4,]    1   0
[5,]    1   1
[6,]    1   1
[7,]    1   1
BIN
  • 781
  • 2
  • 10
  • 24

1 Answers1

0

Thanks guys,now this time I got right. We can group by how many variables you want. I have 2 ways to do that, the first one is not good, the second one is better

> Q1=c(0,1,0,1,0,1,0,1)
> Q2=c(1,0,0,0,1,1,1,0)
> Q3=c(0,0,0,0,0,0,0,0)
> Q4=c(1,0,0,0,1,1,1,0)
> Q5=c(1,0,0,0,1,1,1,0)
> Q6=c(0,0,0,0,0,0,0,0)
> Q7=c(1,0,0,0,1,1,1,0)
> Q8=c(0,0,0,0,0,0,0,0)
> Q9=c(1,0,0,0,1,1,1,0)
> Q = cbind(Q1,Q2, Q3, Q4, Q5, Q6, Q7, Q8, Q9)
> Q = matrix(Q, 8, 9)
> Q
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,]    0    1    0    1    1    0    1    0    1
[2,]    1    0    0    0    0    0    0    0    0
[3,]    0    0    0    0    0    0    0    0    0
[4,]    1    0    0    0    0    0    0    0    0
[5,]    0    1    0    1    1    0    1    0    1
[6,]    1    1    0    1    1    0    1    0    1
[7,]    0    1    0    1    1    0    1    0    1
[8,]    1    0    0    0    0    0    0    0    0

This is the first way

> x <- list(1:3,4:6,7:9)
> do.call(cbind, lapply(x, function(i) ifelse(rowSums(Q[,i]>=1), 1,0)))
     [,1] [,2] [,3]
[1,]    1    1    1
[2,]    1    0    0
[3,]    0    0    0
[4,]    1    0    0
[5,]    1    1    1
[6,]    1    1    1
[7,]    1    1    1
[8,]    1    0    0
> 

This is the second way, it's better

> Q.t <- data.frame(t(Q))
> n <- 3
> Q.t$groups <- rep(seq(1:(ncol(Q)/n)), each = n, len = (ncol(Q)))
> QT <- data.table(Q.t)
> setkey(QT, groups)
> Q.level <- QT[,lapply(.SD,sum), by = groups]
> Q.level <- t(Q.level)
> Q.level <- Q.level[-1,]
> apply(Q.level,2, function(x) ifelse(x>=1,1,0))
   [,1] [,2] [,3]
X1    1    1    1
X2    1    0    0
X3    0    0    0
X4    1    0    0
X5    1    1    1
X6    1    1    1
X7    1    1    1
X8    1    0    0
> 
A. Suliman
  • 12,923
  • 5
  • 24
  • 37
BIN
  • 781
  • 2
  • 10
  • 24
  • The first way seem to be much better. You can improve performance by doing `(sapply(x, function(i) rowSums(Q[, i])) > 0) + 0` for instance. Or `ifelse(sapply(x, function(i) rowSums(Q[, i])) > 0, 1, 0)` – David Arenburg Mar 20 '16 at 08:24
  • I will look into on that – BIN Mar 20 '16 at 09:00