[R] hash table access, vector access &c

Sam Steingold sds at gnu.org
Tue Jul 5 18:53:56 CEST 2011


Hi,
I am confused by the way the indexing works.
I read a table from a csv file like this:

ysmd <- read.csv("ysmd.csv",header=TRUE);
ysmd.table <- hash();
for (i in 1:length(ysmd$X.stock)) ysmd.table[ysmd$X.stock[i]] <- ysmd[i,];

the first column ("X.stock") is a string (factor):

> ysmd$X.stock[[100]]
[1] FLO
7757 Levels: A AA AA- AAAAA AAC AACC AACOU AACOW AADR AAI AAME AAN AAON ... ZZZZT

when I print ysmd.table, I see the data I expect:
...
  ZIOP : ZIOP 402600000 3.03 7.85 707694 6.3717
  ZIP : ZIP 794900000 23.53 31.5 677046 23.2508
  ZIPR : ZIPR 47100000 2.28 3.5 21865 2.4058
  ZIV : ZIV -1 12.2987 17.3862 37455 16.6068
  ZIXI : ZIXI 254900000 2.1 4.88 905849 3.5146
...

moreover,

> ysmd.table[['FLO']]
    X.stock market.cap X52.week.low X52.week.high X3.month.average.daily.volume
100     FLO  2.984e+09      15.3133         22.37                       1021580
    X50.day.moving.average.price
100                      21.3769

quite correctly.
however,

> ysmd.table[ysmd$X.stock[[100]]]
<hash> containing 0 key-value pair(s).
  NA : NULL

so, how do I access the hash table element using non-literal strings?
or, how do I convert ysmd$X.stock[[100]] to a string from whatever it is
now?

thanks!

-- 
Sam Steingold (http://sds.podval.org/) on CentOS release 5.6 (Final) X 11.0.60900031
http://www.PetitionOnline.com/tap12009/ http://ffii.org
http://iris.org.il http://truepeace.org http://camera.org
The dark past once was the bright future.



More information about the R-help mailing list