i want hash like A : 1.2, 3.4, 4.5 B : 9.7, 5.6, 4.8 C : 3.4 ,5.7, 4.6 where A key contain all three value at right sight how to append single keys & values if i have predefined hash with known length -- View this message in context: http://r.789695.n4.nabble.com/how-to-make-hash-append-element-if-i-want-following-condition-tp4409761p4409761.html Sent from the R help mailing list archive at Nabble.com.
how to make hash?& append element, if i want following condition
4 messages · sagarnikam123, R. Michael Weylandt, S Ellison
The easiest way to get a hash structure is to use environments. hash1 <- new.env() hash1$A <- c(1.2, 3.4, 4.5) # etc. If I remember right there is a package on CRAN that abstracts some of this interface away (it's not the most intuitive) but this would likely be the core of it. Michael
On Wed, Feb 22, 2012 at 4:49 AM, sagarnikam123 <sagarnikam123 at gmail.com> wrote:
i want hash like A ?: ?1.2, 3.4, 4.5 B ?: ?9.7, 5.6, 4.8 C ?: ?3.4 ,5.7, 4.6 where A key contain all three value at right sight how to append single keys & values if i have predefined hash with known length -- View this message in context: http://r.789695.n4.nabble.com/how-to-make-hash-append-element-if-i-want-following-condition-tp4409761p4409761.html Sent from the R help mailing list archive at Nabble.com.
______________________________________________ R-help at r-project.org mailing list 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.
i did it, but using hash package ,i got error like
hash1 <- new.env() hash1$A <- c(1.2, 3.4, 4.5) hash1$A
[1] 1.2 3.4 4.5
h<-hash(keys=c("A","B"),values=c(hash1$A,hash1$A) )
Error in .set(h, ...) : Keys of length 2 do not match values of length 6 how should i proceed -- View this message in context: http://r.789695.n4.nabble.com/how-to-make-hash-append-element-if-i-want-following-condition-tp4409761p4410576.html Sent from the R help mailing list archive at Nabble.com.
sagarnikam123 wrote
i did it, but using hash package ,i got error like
hash1 <- new.env() hash1$A <- c(1.2, 3.4, 4.5) hash1$A
[1] 1.2 3.4 4.5
h<-hash(keys=c("A","B"),values=c(hash1$A,hash1$A) )
Error in .set(h, ...) : Keys of length 2 do not match values of length 6 how should i proceed
c(hash1$A,hash1$A) evaluates to ( 1.2, 3.4, 4.5, 1.2, 3.4, 4.5), which is
a vector of length 6. The keys/values form of hash clearly expects the
values element to be of the same length as the keys element.
Unfortunately the environment access methods don;t allow you to return the
element A, only its value. However, if you define hash1 as a list in the
first place (it was meant asd an _alternative_ to hash(), not an addition to
it, I think), you can do
hash1 <- list()
hash1$A <- c(1.2, 3.4, 4.5)
hash(keys=c("A","B"),values=hash1[c('A','A')] )
or
hash(keys=c("A","B"),values=c(hash1['A'],hash1['A']) )
Note that hash1['A'] returns a list with one element, whereas hash1[['A']]
and hash1$A return the value of that element.
Mind you, to make this worthwhile you must have a fairly large data set or a
good reason to require pass-by-reference. The intro to hash says a list will
perform better on smaller data sets, and suggests that you only see
advantages past 1000 key/value pairs.
--
View this message in context: http://r.789695.n4.nabble.com/how-to-make-hash-append-element-if-i-want-following-condition-tp4409761p4410730.html
Sent from the R help mailing list archive at Nabble.com.