[R] to modify a vector

Deepayan Sarkar deepayan at stat.wisc.edu
Sat Feb 8 07:56:03 CET 2003


On Saturday 08 February 2003 12:41 am, Mitsuo Igarashi wrote:
> Hi All.
>
> I am quite a newbie to R.
> This is a basic question.
>
> I like to modify elements of a vector.
> For Example:
> a1 <- c(1,2,3,4,3,5)
>
> TThe following program sentence does not work but the intention is;
>
>   if (a1==3) a1*3 .
>
> 3 in the vector should be changed to 9, and
> the resulted vector is (1,2,9,4,9,5).
>
> How can I get the result?

if you want to store the result in a1 itself,

a1[a1==3] <- a1[a1==3] * 3

Alternately, 

ifelse(a1 == 3, a1 * 3, a1)




More information about the R-help mailing list