1
$\begingroup$

R noob here..

I have the following data frame

>data  
 Value Multiplier
1  15    H
2   0    h
3   2    +
4   2    ?
5   2    k

where the multiplier is of class factor. The values of K & k is 3, + is 5 and ? is 2.

I have used

> data$Multiplier <-  revalue(data$Multiplier, c("+"="5"))
> data$Multiplier <-  revalue(data$Multiplier, c("?"="2"))
> data$Multiplier <-  revalue(data$Multiplier, c("K"="3"))
> data$Multiplier <-  revalue(data$Multiplier, c("k"="3"))  

Is there a better way of doing it?

$\endgroup$
2
$\begingroup$

That seems pretty straight forward to me. I'm pretty new too but in general I'm not sure if you can get better than one command. Though you could have combined all that:

> newValueVector <- c("+"="5", "?"="2", "K"="3", "k"="3")
> data$Multiplier <-  revalue(data$Multiplier, newValueVector)
$\endgroup$
1
  • $\begingroup$ Thanks @amustafa. Now that i think about it, yes I should have been able to figure it out. I think I was caught of guard by the = sign in the string. $\endgroup$ – Drj Oct 17 '15 at 22:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.