hi all, I am trying to use named list to hash a bunch of vector by name, for instance: test = list() test$name = c(1,2,3) the problem is that when i try to get the values back by using the name, the matching isn't done in an exact way, so test$na is not NULL. is there a way around this? Why by default all.equal.list doesnt require an exact match? How can I do hashing in R? thanks. ulas.
You could try using environments: > e <- new.env(hash = TRUE) > e$new <- 1:4 > ls(e) [1] "new" > e$new [1] 1 2 3 4 > e$ne NULL -roger ulas karaoz wrote:> hi all, > I am trying to use named list to hash a bunch of vector by name, for > instance: > test = list() > test$name = c(1,2,3) > > the problem is that when i try to get the values back by using the name, > the matching isn't done in an exact way, so > test$na is not NULL. > > is there a way around this? > Why by default all.equal.list doesnt require an exact match? > How can I do hashing in R? > > thanks. > ulas. > > ______________________________________________ > 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 >-- Roger D. Peng http://www.biostat.jhsph.edu/~rpeng/
On Thu, 18 Nov 2004, ulas karaoz wrote:> hi all, > I am trying to use named list to hash a bunch of vector by name, for > instance: > test = list() > test$name = c(1,2,3) > > the problem is that when i try to get the values back by using the name, the > matching isn't done in an exact way, so > test$na is not NULL. > > is there a way around this??match on the names> Why by default all.equal.list doesnt require an exact match?What do you mean by that? It (by default or not) tests all the attributes, including the names: test <- list(name=1:3) test2 <- list(na=1:3) all.equal(test, test2) [1] "Names: 1 string mismatches" Now, all.equal does not require an exact match, deliberately: that is what identical() is for: identical(test, test2) [1] FALSE> How can I do hashing in R??match -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
ulas karaoz wrote:> is there a way around this?yes...> Why by default all.equal.list doesnt require an exact match?because we're lazy? :)> How can I do hashing in R?you can explicitly test the names for equality, eg with this 2-element list: > x $name [1] 1 2 3 $n [1] 3 2 1 You can do: > x[names(x)=='name'] $name [1] 1 2 3 > x[names(x)=='na'] list() > x[names(x)=='n'] $n [1] 3 2 1 Of course, the right way would be to create a new class, 'hash' perhaps, that did all this in its '$' or '[' methods. Baz
It seems that that behavior is hard-coded in the subscript code, but I bet you could fix it easily by changing the call to get1index offset = get1index(CAR(subs), getAttrib(x, R_NamesSymbol), length(x), /*partial ok*/TRUE, i); in src/main/subset.c (line 762 I think, R-2.0.0) to supply FALSE in place of TRUE and recompiling... I haven't tried yet though so maybe I'm quite badly wrong. Reid Huntsinger -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of ulas karaoz Sent: Thursday, November 18, 2004 11:30 AM To: r-help at stat.math.ethz.ch Subject: [R] hashing using named lists hi all, I am trying to use named list to hash a bunch of vector by name, for instance: test = list() test$name = c(1,2,3) the problem is that when i try to get the values back by using the name, the matching isn't done in an exact way, so test$na is not NULL. is there a way around this? Why by default all.equal.list doesnt require an exact match? How can I do hashing in R? thanks. ulas. ______________________________________________ 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
Use match() for exact matching, i.e., > test[[match("name", names(test))]] Yes, it is more cumbersome. This partial matching is considered by some to be a design fault, but changing it would break too many programs that depend upon it. I don't understand your question about all.equal.list() -- it does seem to require exact matches on names, e.g.: > all.equal(list(a=1:3), list(aa=1:3)) [1] "Names: 1 string mismatches" > all.equal(list(aa=1:3), list(a=1:3)) [1] "Names: 1 string mismatches" > (the above run in R 2.0.0) -- Tony Plate (BTW, in R this operation is generally called "indexing" or "subscripting" or "extraction", but not "hashing". "Hashing" is a specific technique for managing and looking up indices, which is why some other programming languages refer to list-like objects that are indexed by character strings as "hashes". I don't think hashing is used for list names in R, but someone please correct me if I'm wrong! ) At Thursday 09:29 AM 11/18/2004, ulas karaoz wrote:>hi all, >I am trying to use named list to hash a bunch of vector by name, for instance: >test = list() >test$name = c(1,2,3) > >the problem is that when i try to get the values back by using the name, >the matching isn't done in an exact way, so >test$na is not NULL. > >is there a way around this? >Why by default all.equal.list doesnt require an exact match? >How can I do hashing in R? > >thanks. >ulas. > >______________________________________________ >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