0

私はベクトルを持っていますx<-rnorm(100)

次の条件を適用したいと思います。

if any element of x is larger than 2    -> 1.

if any element of x is smaller than -2  -> -1.

otherwise keep x.

私は試した:

ifelse(x>2,1, ifelse(x<-2,-1),x))

しかし、これはうまくいかないようです。私は何を間違っていますか?

4

2 に答える 2

0

ifelseステートメントの形式はifelse(condition, true, false). あなたはあなたの秒ifelseをそのfalse位置に置きたいので、そこにフルを入れてくださいifelse

ifelse(x>2, 1, ifelse(x<-2,-1,x)))
于 2013-10-15T09:19:49.560 に答える