如果元素是同一向量的一部分,如何创建一个值为 1 的二元向量?

发布于 2024-12-14 00:40:04 字数 409 浏览 6 评论 0原文

我想创建一个由二进制文件组成的所谓匹配向量。除非元素属于同一变量,否则所有数字都应为零。

这是一个示例:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

摇划花蜜的午后 2024-12-21 00:40:04

使用 ifelse 函数和 %in% 运算符。

matching_a <-  ifelse(dataset %in% var1, 1, 0)

matching_a
# [1] 1 1 0 0 0 1 1

Using ifelse function and %in% operator.

matching_a <-  ifelse(dataset %in% var1, 1, 0)

matching_a
# [1] 1 1 0 0 0 1 1
〃温暖了心ぐ 2024-12-21 00:40:04
> 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

另外,如果您要进行的匹配比 var1var2 多,则将其扩展为以下内容会很有用:

> 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
> 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 to make than var1 and var2, it'll 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
单身狗的梦 2024-12-21 00:40:04

我看到约翰·科尔比已经采取了我要建议的道路,但我认为我会让它更明确。

二元函数%in% 返回一个逻辑向量并乘以 1,强制转换为“数字” 模式。这也可以通过以下方式完成:

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

matching_a <- 0 + (dataset %in% x)

您还应该查看 %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:

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

matching_a <- 0 + (dataset %in% x)

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

碍人泪离人颜 2024-12-21 00:40:04

我使用了上面约翰的方法(和麦克斯的解决方案)的轻微变化来生成“二进制向量”列表(用于多个匹配),如下所示:

library("plyr")

dataset<-c("a","b","c","d","x","y","z")
var1<-c("a","b","y","z")
var2<-c("c","d","x")
vars <- list(var1, var2)

binaryLst <- lapply(vars ,function(x){ifelse(dataset %in% x, 1, 0)})

输出:

> binaryLst
[[1]]
[1] 1 1 0 0 0 1 1

[[2]]
[1] 0 0 1 1 1 0 0

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:

library("plyr")

dataset<-c("a","b","c","d","x","y","z")
var1<-c("a","b","y","z")
var2<-c("c","d","x")
vars <- list(var1, var2)

binaryLst <- lapply(vars ,function(x){ifelse(dataset %in% x, 1, 0)})

output:

> binaryLst
[[1]]
[1] 1 1 0 0 0 1 1

[[2]]
[1] 0 0 1 1 1 0 0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文