[R] Probably simple function problem
John Kane
jrkrideau at yahoo.ca
Fri Mar 16 22:00:57 CET 2007
# 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.
More information about the R-help
mailing list