[R] hash table access, vector access &c
Sam Steingold
sds at gnu.org
Wed Jul 6 22:44:41 CEST 2011
> * Sam Steingold <fqf at tah.bet> [2011-07-05 15:30:02 -0400]:
>
> I want to modify etr.rt (or create a new frame etr.rt.md) which would
> have all the columns of etr.rt plus 5 additional columns
>
> market.cap
> X52.week.low
> X52.week.high
> X3.month.average.daily.volume
> X50.day.moving.average.price
>
> which for the row number i in etr.rt come from
> ysmd.table[[as.character(etr.rt$symbol[[i]])]]
> (obviously,
>
> etr.rt$symbol[[i]] == ysmd.table[[as.character(etr.rt$symbol[[i]])]]$X.stock
>
> )
this function does the job but it unthinkably slow.
ysmd.extend <- function (fr) {
len <- dim(fr)[1];
fr$mcap <- vector(mode="numeric", len);
fr$lo52 <- vector(mode="numeric", len);
fr$hi52 <- vector(mode="numeric", len);
fr$dvol <- vector(mode="numeric", len);
fr$ma50 <- vector(mode="numeric", len);
for (i in 1:len) {
cat(i," ",fr$symbol[[i]],"(",as.character(fr$symbol[[i]]),")\n")
tmp <- ysmd.table[[as.character(fr$symbol[[i]])]];
fr$mcap[i] <- tmp$market.cap;
fr$lo52[i] <- tmp$X52.week.low;
fr$hi52[i] <- tmp$X52.week.high;
fr$dvol[i] <- tmp$X3.month.average.daily.volume;
fr$ma50[i] <- tmp$X50.day.moving.average.price;
}
fr
}
system.time(etr.rt <- ysmd.extend(etr.rt));
is there a way to do this without the inner for loop?
thanks!
--
Sam Steingold (http://sds.podval.org/) on CentOS release 5.6 (Final) X 11.0.60900031
http://mideasttruth.com http://www.memritv.org http://ffii.org
http://iris.org.il http://dhimmi.com http://memri.org http://palestinefacts.org
Politically Correct Chess: Translucent VS. Transparent.
More information about the R-help
mailing list