Skip to content

Counting variables repeted in dataframe columns to create a presence-absence table

2 messages · Gmail, arun

#
Hi,
Try:
data_m <- read.table(text="Abortusovis07918 Agona08561 Anatum08125 Arizonae65S Braenderup08488
1????? S5305B_IGR S5305B_IGR? S5305B_IGR? S5305B_IGR S5305B_IGR
2????? S5305A_IGR S5300A_IGR? S5305A_IGR? S5300A_IGR S5300A_IGR
3????? S5300A_IGR S5300B_IGR? S5300A_IGR? S5300B_IGR S5300B_IGR
4????? S5300B_IGR S5299B_IGR? S5300B_IGR? S5299B_IGR S5299B_IGR
5????? S5299B_IGR S5299A_IGR? S5299B_IGR? S5829B_IGR S5299A_IGR",sep="",header=TRUE,stringsAsFactors=FALSE)
?data_m$new <-1
library(reshape2)
?dM <- melt(data_m,id.vars="new")
xtabs(new~value+variable,dM)
#or
?dcast(dM,value~variable,value.var="new",fill=0)


A.K.
On Thursday, November 28, 2013 12:18 PM, Gmail <o.irazoki at gmail.com> wrote:
Hi!

I'm new in R and I'm writing you asking for some guidance. I had 
analyzed a comparative genomic microarray data of /56 Salmonella/ 
strains to identify absent genes in each of the serovars, and finally I 
got a matrix that looks like that:
?  Abortusovis07918 Agona08561 Anatum08125 Arizonae65S Braenderup08488
1? ? ?  S5305B_IGR S5305B_IGR? S5305B_IGR? S5305B_IGR S5305B_IGR
2? ? ?  S5305A_IGR S5300A_IGR? S5305A_IGR? S5300A_IGR S5300A_IGR
3? ? ?  S5300A_IGR S5300B_IGR? S5300A_IGR? S5300B_IGR S5300B_IGR
4? ? ?  S5300B_IGR S5299B_IGR? S5300B_IGR? S5299B_IGR S5299B_IGR
5? ? ?  S5299B_IGR S5299A_IGR? S5299B_IGR? S5829B_IGR S5299A_IGR

The variables corresponds to those genes identified as absent in each of 
the serovars. I would like to create a presence-absence matrix of those 
genes comparing all the serovars at the same time, I assume that should 
not be complicated but I don't know how to do it.

I would like a matrix similar to the next one:
? ? ? ? ? ? ?  Abortusovis07918 Agona08561 Anatum08125 Arizonae65S 
Braenderup08488
S5305B_IGR? ? ? ? ? 1? ? ? ? ? ? ? ? 1? ? ? ? ?  1? ? ? ? 1? ? ? 1
S5305A_IGR? ? ? ? ? 1? ? ? ? ? ? ? ? 0? ? ? ? ?  1? ? ? ? 0? ?  0
S5300A_IGR? ? ? ? ? 1? ? ? ? ? ? ? ? 1? ? ? ? ?  1? ? ? ? 1? ? ? 1

Any help would be welcome, and thank you in advance,

Oihane