0

I'm using tvm from the financial package to try to calc number of periods for each row in a data frame

This gives me the correct answer for a single set of inputs:

tvm(i=28.99, pv=3929.02, pmt=-167.61, n=NA)

and the three lines below throw the error I've pasted at the bottom

sam <- data.frame(28.99, 3929.02, 167.61, NA)

sam

z <- tvm(i=sam$X28.99, pv=sam$X2929.02, pmt=-sam$X167.61, n=sam$NA.)

Error in tvm(i = sam$X28.99, pv = sam$X2929.02, pmt = -sam$X167.61, n = sam$NA.) : Incorrect number of Not Available values

Chris Hamson
  • 219
  • 1
  • 2
  • 6
  • Welcome to StackOverflow. Please provide a reproducible example http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Steven Beaupré Sep 12 '15 at 19:10

1 Answers1

0

The problem is here:

sam$X2929.02
# NULL

The name of the column is X3929.02

sam <- data.frame(28.99, 3929.02, 167.61, NA)
tvm(i=sam$X28.99, pv=sam$X3929.02, pmt=-sam$X167.61, n=sam$NA.)
#
# Time Value of Money model
#
#     I% #N      PV FV     PMT Days #Adv P/YR C/YR
# 1 28.99 35 3929.02  0 -167.61   30    0   12   12

I would name the columns of the dataframe so you don't run into this again.

sam <- data.frame(28.99, 3929.02, 167.61, NA)
names(sam) <- c("i", "pv", "pmt", "n")
tvm(i=sam$i, pv=sam$pv, pmt=-sam$pmt, n=sam$n)
#
# Time Value of Money model
#
#     I% #N      PV FV     PMT Days #Adv P/YR C/YR
# 1 28.99 35 3929.02  0 -167.61   30    0   12   12

Or use a list:

sam <- list(i = 28.99, pv = 3929.02, pmt = 167.61, n = NA)
tvm(i=sam$i, pv=sam$pv, pmt=-sam$pmt, n=sam$n)
#
# Time Value of Money model
#
#     I% #N      PV FV     PMT Days #Adv P/YR C/YR
# 1 28.99 35 3929.02  0 -167.61   30    0   12   12
Matt Brigida
  • 191
  • 7
  • thx. worked perfectly. btw, when I scrolled down to read your answer, I saw a link on the side, "How to make a great R reproducible example?" Good reading for people like me. – Chris Hamson Sep 13 '15 at 10:42