-1

I hope you guys are doing well.

I have a quick question. Here is my code:

for(i in 2:49){
  PriceFun[i] = approxfun(data[i]$FUEL_PRICE_REAL ~ data[i]$dd)

  PriceFromDate[i] = function(x) {
    round(PriceFun[i](x), 3) }
} 

What I want the output to be:

PriceFun2 = approxfun(data2$FUEL_PRICE_REAL ~ data2$dd)

PriceFromDate2 = function(x) {
  round(PriceFun2(x), 3) }

PriceFun3 = approxfun(data3$FUEL_PRICE_REAL ~ data3$dd)

PriceFromDate3 = function(x) {
  round(PriceFun3(x), 3) }

and so on...

Is my syntax incorrect? I rarely use R

Thanks in advance.

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
James B
  • 51
  • 7
  • 1
    Please see [how to make a great R reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). As written, it's not clear what you're asking. If you can show some sample data, it would help us understand whether you're trying to loop through a list of data.frames (which would be `data[[i]]` if `data` is your list) or what. – C8H10N4O2 Jan 24 '17 at 01:30
  • I thought my question was pretty clear. There is an issue with the syntax, not with parsing. I simply confused how to use a for loop in R with my example. – James B Jan 24 '17 at 01:36
  • please show us `str(data)` or nobody will be able to help you – C8H10N4O2 Jan 24 '17 at 18:15

1 Answers1

0

1) Would you give more information on the data type of "data" and "PriceFun"? If those are lists, did you make empty lists in front of for loop?

2) I think the body of the function inside the loop would give you the syntax error. If you want to call the elements of the list, try using square bracket [], instead of ().

3) You can round the whole column by just using round(PriceFun[i], 3).

JennyStat
  • 111
  • 6
  • PriceFun looks interpolates data that looks something like this: `data1 [date] [fuel price] [1,] 01/15/2010 3.56 [2,] 01/17/2010 3.32 [3,] 03/18/2010 3.45 [4,] 04/20/2010 3.94 [4,] 04/25/2010 3.94 ` It isn't a list. I simply want to make new variables with the for loop. – James B Jan 24 '17 at 02:15