Skip to content
Prev 141368 / 398498 Next

Replace values according to conditions

mydata[mydata<=3] = -1
mydata[mydata==4 | mydata==5] = 0
mydata[mydata>=6] = 1

See the Intro to R, section 2.7.
There are two versions: | and & that do elementwise logical comparisons on 
vectors; and || and && that are quicker for scalar logical comparisons 
(mostly used in 'if' statement conditions).

See the Intro to R, section 2.4 and 9.2.1.

Regards,
Richie.

Mathematical Sciences Unit
HSL


------------------------------------------------------------------------
ATTENTION:

This message contains privileged and confidential inform...{{dropped:20}}