如果元素是同一向量的一部分,如何创建一个值为 1 的二元向量?
我想创建一个由二进制文件组成的所谓匹配向量。除非元素属于同一变量,否则所有数字都应为零。
这是一个示例:
dataset=("a","b","c","d","x","y","z")
var1=c("a","b","y","z")
var2=c("c","d","x")
因此,我有一个数据集,其中第一行中包含所有变量。现在我创建两个组:var1 和 var2。
元素“a”的匹配向量应该如下所示:
matching_a=c(1,1,0,0,0,1,1)
这些数字对应于我的数据集。如果我的数据集中的变量位于同一组中,则我的匹配向量中应该有 1,否则为 0。
然而,我的实际数据集太大,无法手动完成。有谁明白我想做什么吗?
I would like to create a so-called matching vector consisting of binaries. All numbers should be zero unless elements belong to the same variable.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
ifelse
函数和%in%
运算符。Using
ifelse
function and%in%
operator.另外,如果您要进行的匹配比
var1
和var2
多,则将其扩展为以下内容会很有用:Also, if you have many more matches to make than
var1
andvar2
, it'll be useful to extend this to something like:我看到约翰·科尔比已经采取了我要建议的道路,但我认为我会让它更明确。
二元函数
%in%
返回一个逻辑向量并乘以 1,强制转换为“数字” 模式。这也可以通过以下方式完成:您还应该查看
%in%
函数所基于的?match
。I see that John Colby has already taken the path I was going to suggest, but thought I would make it more explicit.
The dyadic function
%in%
returns a logical vector and multiplying by 1 coerced to "numeric" mode. This could also be done with:You should also look at
?match
on which the%in%
function is based.我使用了上面约翰的方法(和麦克斯的解决方案)的轻微变化来生成“二进制向量”列表(用于多个匹配),如下所示:
输出:
I used a slight variation of John's approach above (and Max's solution) to generate a list of 'binary vectors' (for multiple matches) as follows:
output: