R: Remove the number of occurrences of values in one vector from another vector, but not all

后端 未结 2 1860
自闭症患者
自闭症患者 2021-01-21 18:06

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         


        
相关标签:
2条回答
  • 2021-01-21 18:36

    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
    
    0 讨论(0)
  • 2021-01-21 18:58

    There might be numerous better ways to do it:

    i <- z
    for (val in p) {
      if (val %in% i) {
        i <- i[ - which(i==val)[1] ]
      }
    }
    

    Another solution that I like better because it does not require a test (and thanks fo @Franck's suggestion):

    for (val in p) 
      i <- i[ - match(val, i, nomatch = integer(0) ) ]
    
    0 讨论(0)
提交回复
热议问题