[R] Custom data structures inside of a hash function

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Wed May 4 08:06:30 CEST 2016


It is impolite to put such a minor code fragment on a transient website. Also, this is a plain text mailing list... html usually gets stripped, damaging any advantage the HTML might have had and creating confusion for people reading your message, so please learn how to send plain text.

You wrote:

------
keys <- c('AB', 'AC')
values <- c(c('hello', 0), c('world', 0))
names(values) <- keys
cat(length(values))
-----

You seem to think this is Perl. The c function concatenates... it does not make tuples, it makes vectors, which must have all elements of the same type (unless the vector is of list type). And while vectors can have named elements, those names are not hashes. And since this is R, not Perl, you should use data structures that can be operated on in vectorized fashion, or you will have horrible performance.

So,  the unhelpful but literal answer to your question is

-----
values <- list( list( 'hello', 0 ), list( 'world', 0 ))
names( values ) <- c( "AB", "AC" )
values[[ "AC" ]]
values[[ "AC" ]][[ 2 ]] <- 1
-----

but if you want a scalable solution then you need something like

-----
values <- data.frame( string=c( "hello", "world" ), num=c( 0, 0 ) )
rownames( values ) <- c( "AB", "AC" )
values[ "AC", ]
values[ "AC", 2 ] <- 1
----

though often as not the names get stuffed into another column in the data frame and rows are simply accessed using integer or logical vector indexing.

Note that data frames are column oriented.  This is fundamental to the way R works... embrace it. 

If you absolutely must have a hash then you need environments, but they are tricky animals and are remarkably (if you come from a Perl background) rarely needed. 

A long time ago I came to R with similar thoughts of writing Perl in R... but eventually I read the actual words in the Introduction to R document instead of trying to reframe them in the Perl mind set and only then did I start to make progress with R.

-- 
Sent from my phone. Please excuse my brevity.

On May 3, 2016 6:55:09 PM PDT, "Yves S. Garret" <yoursurrogategod at gmail.com> wrote:
>Hello,
>
>I have the following code:
>http://pastebin.ca/3590201
>
>What I'm trying to do is to create a hash map where the key is two
>characters and
>that maps to a structure of some information.  The problem with the
>above
>example
>is that the first two keys match to the first instance of c('hello',
>0),
>but the second is
>completely ignored.  Ideally, I'd like to retrieve the structure with
>my
>values based on
>the key at hand and modify it as I see fit.
>
>How can I achieve this?
>
>	[[alternative HTML version deleted]]
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

	[[alternative HTML version deleted]]



More information about the R-help mailing list