g**r 发帖数: 425 | 1 If I have the result x=combn(3,3); and y=comb(5,4).
(x=1,2,3; y={1 2 3 4; 1 2 3 5; 1 2 4 5; 1 3 4 5; 2 3 4 5}
How can I remove those columns from y that the first 3 elements=X?
newy={1 2 4 5; 1 3 4 5; 2 3 4 5}
Any help is greatly appreciated...
(Bkgroud:
I have an experiment, and the total number of people in this experiment can
reach n, say 10.
Each result will be just Ber, say with p=0.2.
How ever, if I observe certain results, I will not continue the experiment.
Say if I see 3 out of 3 sucess | y****2 发帖数: 34 | 2 y <- matrix(c(1,2,3,4,1,3,2,5,1,2,3,5,2,3,4,1), nrow=4)
x <- c(1,2,3)
temp <- matrix(match(y, x), nrow=4)
n <- ncol(temp)
index <- rep(0,n)
for(i in 1:n){
comp <- temp[1:length(x),i]== 1:length(x)
index[i] <- i*(sum(comp, na.rm=T)==length(x))
}
ynew <- y[,-index]
You may try this one. I am thinking that the for loop could be replaced by a
more efficient code. (You know, for loop might be slow for large data set) | q**j 发帖数: 10612 | 3 R里面能不能直接比较vector?
x = c(1,2,3)
y = c(4,5,6)
> x == y
[1] FALSE FALSE FALSE
总是一个element的比。 | s*****n 发帖数: 2174 | 4 y <- matrix(c(1,2,3,4,1,3,2,5,1,2,3,5,2,3,4,1), nrow=4)
x <- c(1,2,3)
y[, apply(y[1:3,], 2, function(t) all(t==x))]
can
.
【在 g**r 的大作中提到】 : If I have the result x=combn(3,3); and y=comb(5,4). : (x=1,2,3; y={1 2 3 4; 1 2 3 5; 1 2 4 5; 1 3 4 5; 2 3 4 5} : How can I remove those columns from y that the first 3 elements=X? : newy={1 2 4 5; 1 3 4 5; 2 3 4 5} : Any help is greatly appreciated... : (Bkgroud: : I have an experiment, and the total number of people in this experiment can : reach n, say 10. : Each result will be just Ber, say with p=0.2. : How ever, if I observe certain results, I will not continue the experiment.
| y****2 发帖数: 34 | 5 Yeah, that's exactly what I want. :-) "all" function is nice.
【在 s*****n 的大作中提到】 : y <- matrix(c(1,2,3,4,1,3,2,5,1,2,3,5,2,3,4,1), nrow=4) : x <- c(1,2,3) : y[, apply(y[1:3,], 2, function(t) all(t==x))] : : can : .
| g**r 发帖数: 425 | 6 Thaks; following ur lead, i searched a few more fuctions and the following
seems to work:
x3=combn(3,3)
tx4=combn(5,4)
ttx4=tx4[1:nrow(x3),]
k=0
for (i in 1:ncol(ttx4))
if (sum(apply(y,2,setequal,x=ttx4[,i]))>0 ) k=c(k,-1*i)
x4=tx4[,k]
【在 y****2 的大作中提到】 : y <- matrix(c(1,2,3,4,1,3,2,5,1,2,3,5,2,3,4,1), nrow=4) : x <- c(1,2,3) : temp <- matrix(match(y, x), nrow=4) : n <- ncol(temp) : index <- rep(0,n) : for(i in 1:n){ : comp <- temp[1:length(x),i]== 1:length(x) : index[i] <- i*(sum(comp, na.rm=T)==length(x)) : } : ynew <- y[,-index]
| g**r 发帖数: 425 | 7 nice, thx
【在 s*****n 的大作中提到】 : y <- matrix(c(1,2,3,4,1,3,2,5,1,2,3,5,2,3,4,1), nrow=4) : x <- c(1,2,3) : y[, apply(y[1:3,], 2, function(t) all(t==x))] : : can : .
| q**j 发帖数: 10612 | 8 thanks. just to make sure:
the third argument of apply() is an inline function definition right?
never know that *apply functions can be used this way.
【在 s*****n 的大作中提到】 : y <- matrix(c(1,2,3,4,1,3,2,5,1,2,3,5,2,3,4,1), nrow=4) : x <- c(1,2,3) : y[, apply(y[1:3,], 2, function(t) all(t==x))] : : can : .
| q**j 发帖数: 10612 | 9 and if this works, then my previous question about selecting variables in
one data frame but not in another data frame can be solved as well. |
|