0

I want to calculate the irr for differents columns of a dataframe called tir. So if I do irr(tir$20180201) I get the value.

But i want replace 20180201 with the return of: last(fecha$fecha), that will give me 20180201 or another value.

I do: z <- paste("tir$", last(fecha$fecha), sep="") and I try to do irr(formula(z)) and I get:

Error in parse(text = x, keep.source = FALSE) : 
  <text>:1:5: unexpected numeric constant
1: tir$20181201

How can I solve my problem? Thanks!

Julius Vainora
  • 47,421
  • 9
  • 90
  • 102
  • 1
    Can't quite tell what you're doing. Which `irr` function are you using? (From which package?) Does it not take a data argument? It's unusual to need `data$column` inside a `formula`. And formulas usually need a `~` in them, so the string you're pasting together doesn't look like a formula. – Gregor Thomas Dec 11 '18 at 18:13
  • [This is good background reding](https://stackoverflow.com/q/18222286/903061) for how to access columns in a data frame based on a variable (basically, don't use `$`, use `[` or `[[` instead). `paste`ing code together is possible but rarely a good diea. – Gregor Thomas Dec 11 '18 at 18:16
  • 1
    If I'm right in assuming that you don't need a formula, then `irr[[last(fecha$fecha)]]` should work. Maybe `irr[[as.character(last(fecha$fecha))]]` is needed, can't tell without any sample data...), and we can mark your question as a duplicate of the above link. It would be great if you shared enough details in your question so that we can tell if that's what you want: a little bit of sample data, the expected output, and any packages needed to run the code. – Gregor Thomas Dec 11 '18 at 18:20
  • I use `irr` from the package `FinCal`, if I do: `> irr(tir2$`20180201`)` I get: `[1] 0.002718578` – fededeleon Dec 11 '18 at 18:23
  • I can´t use `irr[[last(fecha$fecha)]]` because `irr[[last(fecha$fecha)]]` only give me the column name of the dataframe `tir`. I need to "merge" `last(fecha$fecha)` with `tir` in the formula `irr(tir$last(fecha$fecha))` or something like that. Thanks for your help. – fededeleon Dec 11 '18 at 18:28
  • 1
    @Gregor I do: `ff <- tir2[as.character(last(fecha$fecha))]` `ff <- as.vector(ff[,1])` `irr(ff)` and I solve my problem. Thanks for the help. – fededeleon Dec 11 '18 at 18:55
  • 1
    Next time please try to show more detail of your data in the question - it will help us answer you much more quickly. – Gregor Thomas Dec 11 '18 at 19:21

0 Answers0