This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Create a presence absence matrix in R for gene data

Hi

I am new to R and would like to create a presence absence matrix for my gene data.

My data at the moment looks like this;

 Isolate    1        2       3        4
 Gene1     N/A     Vp_2003  Vp_2003  Vp_2004
 Gene2    Vp_4590  N/A      N/A      N/A
 Gene3    Vp_3333  Vp_3333  Vp_3333  Vp_3333

....

I would like Vp values to be replaced with 1's and N/A to be replaced with 0's.

Many thanks

r

1 answer

given mat contains your data as character matrix

mat != "N/A"

gives you a logical matrix, that should be ok for binary clustering methods in R.

or

 dist(mat != "N/A", method = "binary" )

gives you the distances directly.

and (mat != "N/A")*1 will get 0s and 1s

Thank you, this worked perfectly

Log in to answer this question.