If you just want the unique geneA
values:
df = structure(c(NA, "c(\"siteX\", \"siteY\")", "siteZ", NA, "0", "0"), .Dim = c(3L, 2L),
.Dimnames = list(c("sample1", "sample2", "sample3"), c("geneA", "gene2")))
df = data.frame(df,stringsAsFactors=F)
df$geneA = as.character(df$geneA)
geneA = unlist(sapply(df$geneA,
function(x)
{ sapply(strsplit(x,",")[[1]],
function(x) { sub(".*\"(\\w+)\".*", "\\1", x,perl=TRUE) } ) }))
names(geneA) = NULL
unique(geneA)
Nicer ordering and cleaning into a data frame with removal of NA geneA's:
df = structure(c(NA, "c(\"siteX\", \"siteY\")", "siteZ", NA, "g1", "g2"), .Dim = c(3L, 2L),
.Dimnames = list(c("sample1", "sample2", "sample3"), c("geneA", "gene2")))
df = data.frame(df,stringsAsFactors=F)
df$geneA = as.character(df$geneA)
require(plyr)
ddply(df, "geneA", function(x)
{
if(!is.na(x))
{
geneAs = sapply(strsplit(x$geneA,",")[[1]], function(y) { sub(".*\"(\\w+)\".*", "\\1", y,perl=TRUE) } );
return(data.frame("geneA"= geneAs, "gene2" = rep(x$gene2[1],length(geneAs)) ))
} else return(NULL)
} )