[R] Creat new column based on condition

Gabor Grothendieck ggrothendieck at gmail.com
Fri Apr 21 22:20:07 CEST 2006


Try:

V1 <- matrix(c(10, 20, 30, 10, 10, 20), nc = 1)

V2 <- 4 * (V1 == 10) + 6 * (V1 == 20) + 10 * (V1 == 30)

or

V2 <- matrix(c(4, 6, 10)[V1/10], nc = 1)

On 4/21/06, Sachin J <sachinj.2006 at yahoo.com> wrote:
> Hi,
>
>  How can I accomplish this task in R?
>
>    V1
>    10
>    20
>    30
>    10
>    10
>    20
>
>  Create a new column V2 such that:
>  If V1 = 10 then V2 = 4
>  If V1 = 20 then V2 = 6
>  V1 =   30 then V2 = 10
>
>  So the O/P looks like this
>
>    V1  V2
>    10   4
>    20   6
>    30  10
>    10   4
>    10   4
>    20   6
>
>  Thanks in advance.
>
>  Sachin
>
> __________________________________________________
>
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list