How to make a mapping function to a vector in R?

I have a vector like (1,100,2,30,20, ...)

And what I would like to do is to compare this value with another value. In fact, I can make a loop for these values, but I wonder if it is possible to make the map function vectorized. For example, I would like to match the value with its bucket, for example, the following

1 -> "< 10", 20 -> "10 to 50", 60 -> "50 to 100", 

thanks

+4
source share
2 answers

How about this?

 test <- c(1,24,2,30,20) sapply( # bin the data up findInterval(test,seq(0,30,10)), # apply a particular function to the data element # dependent on which bin it was put into function(x) switch(x,sqrt(x),sin(x),cos(x),tan(x)) ) [1] 1.0000000 -0.9899925 1.0000000 1.1578213 -0.9899925 
+4
source

Use Purrr map ( https://www.rdocumentation.org/packages/purrr/versions/0.1.0/topics/map )

eg.

 xx=c(1,4,0,3,1) f = function(s) return(c((10*s):(10*s+9))) xx %>% map(f) 
+1
source

All Articles