[R] hash table access, vector access &c
David Winsemius
dwinsemius at comcast.net
Tue Jul 5 19:21:57 CEST 2011
On Jul 5, 2011, at 12:53 PM, Sam Steingold wrote:
> Hi,
> I am confused by the way the indexing works.
Actually I suspect you may be confused by how factors work. See below.
> I read a table from a csv file like this:
>
> ysmd <- read.csv("ysmd.csv",header=TRUE);
# And note that by default all character columns will become factors.
> 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?
Have you considered:
ysmd.table[ as.character( ysmd$X.stock[[100]]) ]
It appears that ysmd$X.stock[[100]] is a factor, and if so, you
probably want the character value that its numeric representation
points to. This is, of course, guesswork because you have not
disclosed what package `hash` comes from, so I do not have the benefit
of looking at its help page.
>
> thanks!
>
> --
> Sam Steingold (http://sds.podval.org/) on CentOS release 5.6 (Final)
> X 11.0.60900031
--
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list