4

(1,100,2,30,20,...) のようなベクトルがあります

そして、私がやりたいのは、これらの値を別の値にマッピングすることです。実際にはこれらの値に対してループを実行できますが、map 関数をベクトル化できるかどうかは疑問です。たとえば、次のように値をそのバケットにマップしたいと思います

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

ありがとう

4

2 に答える 2

4

このようなものはどうですか?

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
于 2012-08-16T04:17:13.060 に答える
2

Purrr を使用するmap( https://www.rdocumentation.org/packages/purrr/versions/0.1.0/topics/map )

例えば

xx=c(1,4,0,3,1)

f = function(s) return(c((10*s):(10*s+9)))
xx %>% map(f)
于 2017-03-31T21:01:03.300 に答える