R:去除向量中二进制元素的分数

时间:2012-07-09 12:33:10

标签: r vector subtraction

想象一下你有一个像

这样的矢量
a <- c(0,0,0,1,1,1)

然后是另一个载体

b <- c(0,1,1)

如何从矢量“a”中删除“b”-vector元素?

输出应该如下:

a (0,0,1) 

2 个答案:

答案 0 :(得分:0)

有趣的问题:

一种天真的做法是

test<-gsub(paste(b,collapse="~"),"",paste(a,collapse="~"))
test<-gsub("~~","~",test)
test<-as.numeric(unlist(strsplit(test,"~")))

编辑:

你可以改变你的采样方法,例如

idx <- sample(length(a))

如果你想拍摄3号样本

a.sample<-a[idx[1:3]]
a.leftover<-a[-idx[1:3]]

,在这种情况下b<-idx[1:3]

答案 1 :(得分:0)

我最初的解释就像ttmaccer,但现在我看到你的评论,你的问题就更简单了。你只想通过B中的项目减少A中的项目数量,并且顺序非常无关紧要。

at <- table(a)
b2 <- c(b, 0, 1) #in case b contains no 1s or 0s
bt <- table(b2) - c(1,1)
abt <- at - bt
rep(0:1, abt)
相关问题