[R] Help reading table rows into lists

Gabor Grothendieck ggrothendieck at gmail.com
Sun Oct 10 21:56:13 CEST 2010


On Sun, Oct 10, 2010 at 2:59 PM, Jeffrey Spies <jspies at virginia.edu> wrote:
> To get just the list you wanted, Gabor's solution is more elegant, but
> here's another using the apply family.  First, your data:
>
> dat <- scan(file="/g/bork8/waller/test_COGtoPath.txt",what="character",sep="\n")
>
> I expect dat to be a vector of strings where each string is a line of
> values separated by tabs, which I think, by looking at your other
> code, is what you get.
>
> sapply(dat, function(x){
>    tmp<-unlist(strsplit(x, '\t', fixed=T))
>    out <- list(tmp[seq_along(tmp)[-1]])
>    names(out) <- tmp[1]
>    out
> }, USE.NAMES=F)
>
> The one difference between the two is that if you have a COG with no
> pathways (might not be realistic or that big of a deal), this solution
> will have the COG name in the list with a value of character(0) where
> Gabor's will omit the COG completely. Again, probably not a big deal.

If that is important then do it this way:

Lines <- "COG0001 patha   pathb   pathc
COG0002 pathd   pathe
COG0003 pathe   pathf   pathg   pathh
COG0004"
DF <- read.table(textConnection(Lines), header = FALSE,
        fill = TRUE, as.is = TRUE, na.strings = "")

library(reshape2)
m <- melt(DF, 1)
lapply(unstack(m, value ~ V1), complete.cases)

acast(m, value ~ V1)	


-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list