[R] Probably simple function problem

Jason Barnhart jasoncbarnhart at msn.com
Fri Mar 16 23:05:57 CET 2007


You didn't specify the exact nature of the problem so I guess that you 
want it return the vector cppm.

Add "return(cppm)" as the final line in function.

Here's what I think you want; it replicates your original code.

> adjusts <- c(.50, .70, .29, .27 , .40 , .26 , 125)
> coal <- 1:6
> newdata <- 1:10
>
> fuel.costing <- function(fuel, utr, mydata) {
+ cppf <- cppm <- fuel ;
+ cppf[2] <- fuel[2]*(1-utr[2])*length(mydata) + utr[7]*
+ utr[2]*utr[5] ;
+ cppf[4] <- fuel[2]*(1-utr[4])*length(mydata) + utr[7]*
+ utr[4]*utr[6] ;
+ cppm[2] <- fuel[2]*(1-utr[1])*length(mydata) ;
+ cppm[4] <- fuel[2]*(1-utr[3])*length(mydata)
+ return(cppm)
+ }
>
> my.cppm <- fuel.costing(coal, adjusts, newdata)
> my.cppm
[1]  1.0 10.0  3.0 14.2  5.0  6.0

HTH
-jason

----- Original Message ----- 
From: "John Kane" <jrkrideau at yahoo.ca>
To: "R R-help" <r-help at stat.math.ethz.ch>
Sent: Friday, March 16, 2007 2:00 PM
Subject: [R] Probably simple function problem


># I have a simple function problem. I thought that I
> could write a function to modify a couple of vectors
> but I am doing something wrong
>
> #I have a standard cost vector called "fuel" and some
> adjustments to the
> #costs called "adjusts".  The changes are completely
> dependend on the length
> #of the dataframe newdata  I then need to take the
> modifed vectors and use
> # them later. I need to do this several times and the
> only change in the variables
> # is the length of the data.frame.
>
> # Can anyone suggest what I am doing wrong or am I
> just misunderstanding what
> # a function is supposed to do?
>
> #Example:
>
> adjusts <- c(.50, .70, .29, .27 , .40 , .26 , 125)
> coal <- 1:6
> newdata <- 1:10
>
> fuel.costing <- function(fuel, utr, mydata) {
> cppf <- cppm <- fuel ;
> cppf[2] <- fuel[2]*(1-utr[2])*length(mydata) + utr[7]*
> utr[2]*utr[5] ;
> cppf[4] <- fuel[2]*(1-utr[4])*length(mydata) + utr[7]*
> utr[4]*utr[6] ;
> cppm[2] <- fuel[2]*(1-utr[1])*length(mydata) ;
> cppm[4] <- fuel[2]*(1-utr[3])*length(mydata)
> }
>
> fuel.costing(coal, adjusts, newdata)
>
>
> ## original code for one place
> cppf <- cppm <- coal ;
> cppf[2] <- coal[2]*(1-adjusts[2])*length(newdata) +
> adjusts[7]* adjusts[2]*adjusts[5] ;
> cppf[4] <- coal[2]*(1-adjusts[4])*length(newdata) +
> adjusts[7]* adjusts[4]*adjusts[6] ;
> cppm[2] <- coal[2]*(1-adjusts[1])*length(newdata) ;
> cppm[4] <- coal[2]*(1-adjusts[3])*length(newdata)
>
> label(cppm) <- "cppm - > SW coal costs adjusted "
> label (cppf) <- "cppf -> WW coal costs adjusted "
>
> # Any help or suggests would be greatly appreciated.
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list