[R] list entries file into a list
Albert Vilella
avilella at gmail.com
Fri Jan 27 19:13:37 CET 2006
This worked great but I'm a bit confused about how to access the names
(keys?) of a list in a loop, and why this is failing for me:
I want to cross the entries in "mylist" (like "entry0001") with the
columns of a dataframe "ginput":
$entry0001
> [1] "AB0032" "CF32134" "DF34334"
>
> $entry0002
> [1] "AB0033"
>
> $entry0003
> [1] "AB0032" "CF32134" "DF34334" "DD343434" "DD34222"
>
> $entry0004
> [1] "AB0032" "CF32134"
> ginput
mid reffile pa
1 myetag0001 AB0032 0.778270
2 myetag0002 AB0032 0.153093
3 myetag0003 AB0032 0.392175
4 myetag0004 AB0032 0.696303
5 myetag0005 AB0032 0.688537
6 myetag0006 AB0032 0.767044
7 myetag0001 AB0033 0.420000
8 myetag0002 AB0033 0.330333
9 myetag0003 AB0033 0.238013
10 myetag0004 AB0033 0.043213
...
The rough idea has been to do something like:
mynames = rep(0,length(names(mylist)))
mypas = rep(0,length(names(mylist)))
i = 0
for (entrylabel in names(mylist)) {
mynames[i] = entrylabel
mypas[i] =+ sum(ginput$pa[ginput$reffile %in% mylist$entrylabel])
i = i+1
}
But mylist$entrylabel is not working inside the loop.
I checked that entrylabel returns:
[1] "entry0001"
And that mylist$entry0001 is returning the right list.
I would like to end up with something like:
dfpa = data.frame(dfnames=mynames,dfpas=mypas)
> dfpa
dfnames dfpas
1 entry0001 3.475422
2 entry0002 2.221355
...
I haven't been able to found my way with R lists, maybe because I'm
comparing them with perl's hashes.
Any idea of what I'm missing?
Albert.
El dj 26 de 01 del 2006 a les 08:14 -0500, en/na Liaw, Andy va escriure:
> The following might be what you want (replace "clipboard" with your
> filename):
>
> > mylist <- strsplit(readLines("clipboard"), ":")
> > nm <- sapply(mylist, "[", 1)
> > mylist <- lapply(mylist, "[", -1)
> > names(mylist) <- nm
> > mylist <- lapply(mylist, function(s) strsplit(s, ",")[[1]])
> > mylist
> $entry0001
> [1] "AB0032" "CF32134" "DF34334"
>
> $entry0002
> [1] "AB0033"
>
> $entry0003
> [1] "AB0032" "CF32134" "DF34334" "DD343434" "DD34222"
>
> $entry0004
> [1] "AB0032" "CF32134"
>
> Andy
>
> From: Albert Vilella
> >
> > Hi all,
> >
> > I have a file of this kind:
> >
> > entry0001:AB0032,CF32134,DF34334
> > entry0002:AB0033
> > entry0003:AB0032,CF32134,DF34334,DD343434,DD34222
> > entry0004:AB0032,CF32134
> >
> > And I would like to read it into something like a hash, so that I can
> > then loop over it by keys and values.
> >
> > I wonder which would be the best way to do that in R?
> >
> > Thanks,
> >
> > Albert.
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide!
> > http://www.R-project.org/posting-guide.html
> >
> >
>
>
> ------------------------------------------------------------------------------
> Notice: This e-mail message, together with any attachment...{{dropped}}
More information about the R-help
mailing list