Comparing Vectors Values: 1 element with all other

后端 未结 2 879
一向
一向 2021-01-20 05:44

I\'m wondering how I can compare 1 element of a vector with all elements in the other vector. As an example: suppose

x <- c(1:10)  
y <- c(10,11,12,13,         


        
2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-20 06:46

    If you want to compare each element of x to y, usually one of the 'apply' functions will help.

    As follows:

    x <- c(1:10)

    y <- c(10,11,12,13,14,1,7)

    sapply(x,function(z){z==y})

    Column i in the output is result from x[i]==y.

    Is this what you're looking for?

提交回复
热议问题