using a dictionary in R
On Wed, Oct 19, 2011 at 11:06:21AM +0200, Eran Eidinger wrote:
Hi all,
Is there a way to create a "dictionary" in R, such that it has <key,value>
pairs?
Something to the effect of:
x=dictionary(c("Hi","Why","water") , c(1,5,4))
x["Why"]=5
You can do that with named elements of a vector:
foo <- c(1,5,4)
names(foo) <- c("Hi","Why","water")
foo
Hi Why water
1 5 4
foo["Why"]
Why 5
foo["Why"] <- 8 foo
Hi Why water
1 8 4
I want to compute x1^2+x2 on all combinations of x keys
x1 x2 val1 val2 x1^2+x2
1 a a 5 5 30
2 b a 2 5 9
3 a b 5 2 27
4 b b 2 2 6
I guess this is along the lines you have in mind:
dat <- expand.grid(c('a','b'), c('a','b'))
dat$a <- foo[dat$Var1]
dat$b <- foo[dat$Var2]
dat
Var1 Var2 a b 1 a a 1 1 2 b a 8 1 3 a b 1 8 4 b b 8 8
dat$prod <- dat$a*dat$b dat
Var1 Var2 a b prod 1 a a 1 1 1 2 b a 8 1 8 3 a b 1 8 8 4 b b 8 8 64
get_result["b","a"] = 9
I'm not sure what exactly you want here... cu Philipp
LipoFIT Analytic GmbH Dr. Philipp Pagel Josef-Engert-Str. 9 93053 Regensburg, Germany Phone: +49 941 698 091 04 Fax: +49 941 698 091 01 Sekretariat: +49 941 698 091 00 Email: philipp.pagel at lipofit.de Web: www.lipofit.de --- Rechtsform: GmbH Gesch?ftsf?hrer: Dr. Fritz Huber, Dr. Volker Pfahlert Sitz: Regensburg, Amtsgericht Regensburg, HRB 9368 Steuernummer: 183/55803