I have the following function
FEType <- function(NoGears ){
ret="Low"
if ((NoGears == 1) ) {ret ="Med"}
return(ret)
}
and I call it using
df <- as.data.frame.matrix(trainingdata)
#No.Gears is an integer
df$FEType = FEType( df$No.Gears )
I get
Warning message in if ((NoGears == 1)) {: "the condition has length > 1 and only the first element will be used"
According to this question I need to use a vector But I am not seeing how to send a vector to my function.
Update
I was able to do the following;
FEType <- function(v ){
ret="Low"
if ((v[1] == 1) ) {ret ="Med"}
return(ret)
}
and call it via
df$FEType = FEType( c(df$No.Gears)
The code is less than satisfying to read. How do I do this in a more readable manner?
[Update]
One answer is to use a names vector
FEType <- function(v ,n ){
ret="Low"
names(v ) <-n
if ((v["NoGears"] == 1) ) {ret ="Med"}
return(ret)
}
I have asked here to give context to my problem.