Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

make binary (presence/absence) data matrix from multiple lists in r

I have a series of separate variable lists (character strings) that are different lengths. I want to combine them into one data frame to make a presence (1)/absence (0) matrix. Given that they are different lengths, I can't figure out how to even create the initial data frame. Here my example:

data1 <- c("a", "b", "c", "d", "e", "f")
data2 <- c("e", "f", "g")
data3 <- c("a", "c", "g")

My final result I would like to create a binary presence/absence matrix as below so I can create a graphic (similar to a heatmap) to display this.

   data1     data2    data3
a    1        0         1
b    1        0         0
c    1        0         1   
d    1        0         0
e    1        1         0
f    1        1         0
g    0        1         1

I'm still new to R so hope my explanation is okay. Thanks for the help.

like image 973
KNN Avatar asked Nov 17 '25 06:11

KNN


1 Answers

There is a helper function in the splitstackshape package called charMat that you might want to give a try

dat <- paste0("data", 1:3)
out <- t(splitstackshape:::charMat(listOfValues = mget(dat), fill = 0L))
colnames(out) <- dat
out
#  data1 data2 data3
#a     1     0     1
#b     1     0     0
#c     1     0     1
#d     1     0     0
#e     1     1     0
#f     1     1     0
#g     0     1     1

data

data1 <- c("a", "b", "c", "d", "e", "f")
data2 <- c("e", "f", "g")
data3 <- c("a", "c", "g")

explanation

The function expects a list as first argument. We can use mget to create that list

mget(dat)
#$data1
#[1] "a" "b" "c" "d" "e" "f"

#$data2
#[1] "e" "f" "g"

#$data3
#[1] "a" "c" "g"

where dat is a character vector that contains the names of your input data

dat
#[1] "data1" "data2" "data3"

t is used to transpose the output of charMat.

Hope this helps.

like image 197
markus Avatar answered Nov 19 '25 21:11

markus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!