[R] constructing a vector from a dataframe and another vector

Jeff Miller jdm at xnet.com
Thu Feb 1 05:22:01 CET 2001


Hi All,

I have a dataframe, divs, that looks like this:


>  divs
          date             ticker         dividend
263     20010322    ADBE      0.025
264     20010628    ADBE      0.025
265     20010927    ADBE      0.025
4308   20010212     ED          0.550
4309   20010514     ED          0.410
5416   20010330     GE          0.137
5417   20010629     GE          0.137
5418   20010928     GE          0.137

and a character vector of symbols that might, for example,  look like this:

syms <- c("ADBE", "AAPL", "ED", "ED", "ADBE")

>From these two data objects, I'd like to construct the following numeric
vector of dividends:

 divvec <-   c( .025, .025, .025, .550, .410, .550, .410, .550, .410, .025,
.025, .025)

In other words, I'd like to construct a numeric vector by replacing each
symbol in syms
by the dividends associated with that symbol in the dataframe divs.

Symbols can occur more than once in syms, and some of the symbols in syms
might not
be in divs.

I see that if each symbol  occurred only once in syms, it's just a matter of
re-ordering divs
and using as.vector(divs[,"dividend"]). I don't see how to proceed when
symbols
occur more than once in syms.

Is there a clean way to do this?

Many thanks in advance,

        Jeff Miller






-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list