Comparing character vectors in R to find unique and/or missing values

前端 未结 3 531
逝去的感伤
逝去的感伤 2021-02-01 19:32

I have two character vectors, x and y.

x <- c(\"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\")
y <- c(\"a\",      \"c\", \"d\", \"e\",      \"g\")
<         


        
相关标签:
3条回答
  • 2021-02-01 20:11

    I think this should work:

    x[!(x %in% y)]
    

    First it checks for all x that are not in y, then it uses that as an index on the original.

    0 讨论(0)
  • 2021-02-01 20:13
    setdiff(x,y)
    

    Will do the job for you.

    0 讨论(0)
  • 2021-02-01 20:19
    > x[!x %in% y]
    [1] "b" "f"
    

    or:

    > x[-match(y,x)]
    [1] "b" "f"
    > 
    
    0 讨论(0)
提交回复
热议问题