Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using sapply with switch

Tags:

r

Lets say I have the following case:

I am trying to apply a switch statement to each term in test where test = c("AA","bb") and mapping = c("AA"=5,"bb"=7)

If I do

sapply(test, switch, mapping )

I get

    AA bb
AA  5  5
bb  7  7

instead of c(5,7) like I want. Is there any way to modify sapply(test,switch,...) such that the first 2 arguments are still test and switch and I am able to pass in a vector for the mapping?

like image 509
user1431282 Avatar asked Dec 27 '22 06:12

user1431282


1 Answers

I'll make it an answer then, you can just do:

mapping[test]
# AA bb 
# 5  7 
like image 100
flodel Avatar answered Dec 28 '22 20:12

flodel