[R] how to make hash?& append element, if i want following condition
slre
S.Ellison at lgc.co.uk
Wed Feb 22 16:32:48 CET 2012
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.
More information about the R-help
mailing list