如何创建具有1的二元载体,如果元素是相同的载体的一部分? [英] How to create a binary vector with 1 if elements are part of the same vector?
问题描述
我想创建一个包含二进制文件的R中所谓的匹配向量。的所有数字应为零除非元素属于同一个变量。听起来令人困惑?它是! :)
这里有一个例子:
数据集=(A,B,C,D,×,Y,Z)
VAR1 = C(一,B,Y,Z)
VAR2 = C(C,D,×)
因此,我与在第一行中的所有变量的数据集。现在,我创建两个组:VAR1和var2。
该元素的匹配矢量A应该是这样的:
matching_a = C(1,1,0,0,0,1,1)
的数字对应于我的数据集。如果在我的数据集的变量是在同一个组,应该有在我的匹配矢量a 1,和一个0否则
不过,我的实际数据集是太大,做手工。有谁明白我想做的事?你能帮忙吗?
非常感谢事先!
> matching_a = ifelse(在%VAR1数据集中%,1,0)
> matching_a
[1] 1 1 0 0 0 1 1
I would like to create a so-called matching vector in R consisting of binaries. All numbers should be zero unless elements belong to the same variable. Sounds confusing? It is! :) Here's an example:
dataset=("a","b","c","d","x","y","z")
var1=c("a","b","y","z")
var2=c("c","d","x")
Thus, I have a dataset with all the variables in the first line. Now I create two groups: var1 and var2. The matching vector for the element "a" is supposed to look like:
matching_a=c(1,1,0,0,0,1,1)
The numbers correspond to my dataset. If the variables in my dataset are in the same group, there should be a 1 in my matching vector, and a 0 otherwise.
However, my actual data set is too big to do it manually. Does anyone understand what I wanna do? And can you help?
Thanks a lot in advance!
> matching_a = ifelse(dataset %in% var1, 1, 0)
> matching_a
[1] 1 1 0 0 0 1 1
这篇关于如何创建具有1的二元载体,如果元素是相同的载体的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!