[R] vectorizing problem

Dylan Miracle dylan.miracle at gmail.com
Mon Oct 4 19:54:17 CEST 2010


Hello,

I have a two column dataframe that
has entries that look like this:

2315100       NR_024005,NR_024004,AK093685
2315106       DQ786314

and I want to change this to look like this:

2315100       NR_024005
2315100       NR_024004
2315100       AK093685
2315106       DQ786314

I can do this with the following "for" loop but the dataframe (GPL)
has ~140,000 rows and this takes about 15 minutes:


extGPL <- matrix(nrow=0,ncol=2)
for (i in 1:length(GPL[,2])){
       aa <- unlist(strsplit(as.character(GPL[i,2]),"\\,"))
       bb <- rep(as.numeric(as.character(GPL[i,1])), length(aa))
       cc <- matrix(c(bb,aa),ncol = 2)
       extGPL <- rbind(extGPL,cc)
}

Is there a way to vectorize this?

Thanks,

Dylan Miracle
University of Minnesota
GCD Department



More information about the R-help mailing list