首页 > 解决方案 > Character to vector in R

问题描述

I have 3 binary vectors.

a<-c(0,0,1,0)
b<-c(1,0,0,0)
c<-c(0,0,0,1)

I need to choose 2 out of 3 vectors and check whether they are subsets. I have used combn function as bellow:

> combination<-combn(letters[1:3], 2)
> combination
     [,1] [,2] [,3]
[1,] "a"  "a"  "b" 
[2,] "b"  "c"  "c" 

But the problem is- all the elements in combination are characters. How can I connect these elements (which are letters) to the binary vector?

For example if I call combination[1,1] I can get binary vector a (not "a").

标签: r

解决方案


您可以使用get名称搜索对象

get(combination[1,1])
#[1] 0 0 1 0

mget更多:

do.call(cbind, mget(combination[,1]))
#     a b
#[1,] 0 1
#[2,] 0 0
#[3,] 1 0
#[4,] 0 0

推荐阅读