R:从另一个向量中删除一个向量中值出现的次数,但不是全部 [英] R: Remove the number of occurrences of values in one vector from another vector, but not all

查看:34
本文介绍了R:从另一个向量中删除一个向量中值出现的次数,但不是全部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为令人困惑的标题道歉,但我不知道如何表达我的问题.在 R 中,我想解决以下问题:

Apologies for the confusing title, but I don't know how to express my problem otherwise. In R, I have the following problem which I want to solve:

x <- seq(1,1, length.out=10)
y <- seq(0,0, length.out=10)
z <- c(x, y)
p <- c(1,0,1,1,0,0)

我如何从向量 z 中删除向量 p 以便向量一个新向量 i 现在有 3 次出现 1 和 3 次出现 0 less,那么我必须做什么才能得到以下结果结果?在解决方案中,z 中 1 和 0 的顺序应该无关紧要,它们可能只是随机顺序,而且还可能涉及其他数字.

How can I remove vector p from vector z so that vector a new vector i now has three occurrences of 1 and three occurrences 0 less, so what do I have to do to arrive at the following result? In the solution, the order of 1's and 0's in z should not matter, they just might have been in a random order, plus there can be other numbers involved as well.

i
> 1 1 1 1 1 1 1 0 0 0 0 0 0 0

提前致谢!

推荐答案

类似于@VincentGuillemot 的回答,但采用函数式编程风格.使用 purrr 包:

Similar to @VincentGuillemot's answer, but in functional programming style. Uses purrr package:

i <- z
map(p, function(x) { i <<- i[-min(which(i == x))]})
i

> i
 [1] 1 1 1 1 1 1 1 0 0 0 0 0 0 0

这篇关于R:从另一个向量中删除一个向量中值出现的次数,但不是全部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆