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

Bill.Venables@CMIS.CSIRO.AU Bill.Venables at CMIS.CSIRO.AU
Thu Feb 1 06:32:39 CET 2001



| -----Original Message-----
| From: Jeff Miller [mailto:jdm at xnet.com]
| Sent: Thursday, 1 February 2001 14:22
| To: r-help at stat.math.ethz.ch
| Subject: [R] constructing a vector from a dataframe and another vector
| 
| 
| 
| 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?

Let's take it in stages.  First split up the dividends:

> divvy.up <- split(divs$dividend, divs$ticker)

Now check which values in syms belong to the set you have:

> m <- is.element(syms, levels(divs$ticker))

Now put together your vector:

> divvec <- unlist(divvy.up[syms[m]])

Easy as 1, 2, 3.  Or, if you want it all in one large, 
undigestible lump:

> divvec <- unlist(split(divs$dividend, 
	divs$ticker)[syms[is.element(syms, levels(divs$ticker))]])

(Warning: untested code.)


--
Bill Venables, CSIRO/CMIS Environmetrics Project
Email: Bill.Venables at cmis.csiro.au
Phone: +61 7 3826 7251
Fax:   +61 7 3826 7304
Postal: PO Box 120, Cleveland, Qld 4163, AUSTRALIA



| 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
| _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
| _._._._._._._._._
| 
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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