For the following dataset, I wrote a function,
expconvert <- function(a) {
if(a=="h" || a=="H")
return(100)
if(a=="k" || a=="K")
return(1000)
if(a=="m" || a=="M")
return(1000000)
if(a=="b" || a=="B")
return(1000000000)
if(is.numeric(a))
return(a)
else
return(0)
}
The data set looks like following,
CROPDMGEXP CROPDMG PROPDMG PROPDMGEXP
k 0 20 h
H 23 41 B
k 10 5 B
2 3 k
5 50
The transformed data set should look like following,
CROPDMGEXP CROPDMG PROPDMG PROPDMGEXP
1000 0 20 100
100 23 41 1000000000
1000 10 5 1000000000
0 2 3 1000
0 5 50 0
I wish to apply the above function to the first and the last column. When I write the following code, consider df
as the above data frame
df[c(1,4)] <- apply(df[c(1,4)], MARGIN = 1, FUN = expconvert)
I don't get the desired output that is the conversion of the letters in those columns to appropriate numerical weights.
But when I use apply
for individual column it works fine as below,
df$CROPDMGEXP <- apply(df[1], MARGIN = 1, FUN = expconvert)
Please help me how do I apply it to both the columns at the same time.
There are many levels in the data set so setNames is cool when there are few. That is why I wrote the function. The question is the function works fine for single column with apply, but returns wrong values when used with multiple columns with apply.