How to create a binary vector with 1 if the elements are part of the same vector?

I would like to create a so-called correspondence vector consisting of binary files. All numbers must be zero if the elements do not belong to the same variable.

Here is an example:

dataset=("a","b","c","d","x","y","z") var1=c("a","b","y","z") var2=c("c","d","x") 

So I have a dataset with all the variables in the first row. Now I create two groups: var1 and var2.

The corresponding vector for element "a" should look like this:

 matching_a=c(1,1,0,0,0,1,1) 

The numbers correspond to my data set. If the variables in my dataset are in the same group, there should be 1 in my matching vector, otherwise 0.

However, my actual dataset is too large to do it manually. Does anyone understand what I want to do?

+6
source share
3 answers

Using the ifelse function and the %in% operator.

 matching_a <- ifelse(dataset %in% var1, 1, 0) matching_a # [1] 1 1 0 0 0 1 1 
+4
source
 > output1 = 1 * dataset %in% var1 > output2 = 1 * dataset %in% var2 > output1 [1] 1 1 0 0 0 1 1 > output2 [1] 0 0 1 1 1 0 0 

Also, if you have many more matches than var1 and var2 , it would be useful to extend this to something like:

 > vars = list(var1, var2) > 1 * sapply(vars, function(x) dataset %in% x) [,1] [,2] [1,] 1 0 [2,] 1 0 [3,] 0 1 [4,] 0 1 [5,] 0 1 [6,] 1 0 [7,] 1 0 
+4
source

I see that John Colby has already taken the path I was about to offer, but thought that I would make it more explicit.

The dyadic function %in% returns a logical vector and is multiplied by 1, forcibly by the "numerical" mode. This can also be done with:

 matching_a <- as.numeric(dataset %in% x) # Or matching_a <- 0 + (dataset %in% x) 

You should also look at ?match , which the %in% function is based on.

+3
source

Source: https://habr.com/ru/post/900850/


All Articles