0

I created an R object resultant from two tapply operations:

#getting first offer date for each fund
firstOfferDtByFund <- tapply(allEMNLSEFundsWithSymbols$first_offer_dt, allEMNLSEFundsWithSymbols$Symbol, min)

head(firstOfferDtByFund)
        AANNX    AASAX    AASFX    ACDHX    ACDJX 
  NA 20100803 20081105 20080303 20111031 20111031 


#getting the last date for each fund
lastDtByFund <- tapply(allEMNLSEFundsWithSymbols$caldt, allEMNLSEFundsWithSymbols$Symbol, max)

head(lastDtByFund)
        AANNX    AASAX    AASFX    ACDHX    ACDJX 
  NA 20131231 20121231 20121231 20131231 20131231 

I then combine those two objects to create an object with first and last dates available for various mutual fund data.

#now to combine the output
fundDateRange <- (cbind(firstOfferDtByFund,lastDtByFund))

head(fundDateRange)


     firstOfferDtByFund lastDtByFund
                      NA           NA
AANNX           20100803     20131231
AASAX           20081105     20121231
AASFX           20080303     20121231
ACDHX           20111031     20131231
ACDJX           20111031     20131231

All works well, but when I attempt to assign the mutual fund symbols to the combined object, I get a coercing message that system is attempting to coerce to a list

fundDateRange$Symbol <- rownames(fundDateRange)

Warning message:

In fundDateRange$Symbol <- rownames(fundDateRange) : Coercing LHS to a list

I am not trying to create a list, only a matrix or data.frame so I have can have mutual fund symbols aligned with beginning and ending data availability. I performed this operation a few weeks ago without that coercing warning. How do I create a data frame that contains mutual fund symbols and the associated dates without creating a list?

Heroka
  • 12,889
  • 1
  • 28
  • 38
fibrou
  • 313
  • 1
  • 5
  • 15
  • I provide edits to include header information from each object. How else can I help you help me? – fibrou Dec 08 '15 at 18:05
  • Can you show the `str(fundDateRange)` – akrun Dec 08 '15 at 18:08
  • > str(fundDateRange) int [1:575, 1:2] NA 20100803 20081105 20080303 20111031 20111031 20111031 20111031 20111031 20111031 ... - attr(*, "dimnames")=List of 2 ..$ : chr [1:575] "" "AANNX" "AASAX" "AASFX" ... ..$ : chr [1:2] "firstOfferDtByFund" "lastDtByFund" – fibrou Dec 08 '15 at 18:26
  • Looks like you have a matrix. There fore, the `$` won't work. You can do `transform(as.data.frame(fundDateRange), Symbol=rownames(fundDateRange))` – akrun Dec 08 '15 at 18:29
  • 1
    Please update your example to make it reproducible (please provide samples of each relevant dataset, e.g. using `dput`). For details, please see: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – josliber Dec 08 '15 at 18:39
  • Thanks @akrun. That was the ticket! – fibrou Dec 08 '15 at 19:43
  • @akrun - it is interesting, though, that I did not have that problem about 3 weeks ago. I just updated my version of R. Do you think that may have caused the message? Thanks again to all! – fibrou Dec 08 '15 at 19:45
  • In the recent versions that I know of if the dataset is a `matrix` then doing, fundDateRange$Symbol <- ...` should come up with warning (though with a different warning.). As you didn't show an example of the original dataset, i.e. `allEMNLSEFundsWithSymbols`, it is hard to say what might be the problem. Please use `dput` as josilber suggested. – akrun Dec 09 '15 at 02:45

1 Answers1

1

We can try

library(dplyr)
allEMNLSEFundsWithSymbols %>%
      group_by(Symbol) %>% 
      summarise(Min = min(first_offer_dt), Max = max(caldt))
akrun
  • 874,273
  • 37
  • 540
  • 662
  • not using dplyr package. The above steps / lines of code worked without the warning or coercing message. I don't know what changed. – fibrou Dec 08 '15 at 18:27
  • Looks like I may have to teach myself dplyr @akrun. Thanks for the suggestion. – fibrou Dec 08 '15 at 19:47
  • @fibrou What changed is `tapply` provided a named vector output for each run. When you `cbind` any two vectors, it becomes a `matrix` and you can't do `$` to assign with matrix. Here, we are summarising the dataset without changing from one class to another ie. data.frame remains the same. – akrun Dec 09 '15 at 02:41