[R] Creating new variables

jim holtman jholtman at gmail.com
Sun Oct 11 00:32:18 CEST 2009


If the 'data set' is a dataframe, the following will work:

x$v1 <- ifelse(x$p > 0.4, 1, 0)
x$v2 <- ifelse(x$p > 0.6, 1, 0)

If it is matrix, try

x <- cbind(x, v1=ifelse(x[,'p'] > 0.4, 1, 0), v2=ifelse(x[,'p'] > 0.6, 1, 2))

If helps a lot if you follow the posting rules and  provide commented,
minimal, self-contained, reproducible code.

On Sat, Oct 10, 2009 at 6:04 PM, Ashta <sewashm at gmail.com> wrote:
> Hi all,
>
> I have a data set called x with    200 rows  and 12  columns.  I want
> create  two more columns based on  probability. ie
>  if p >0 .4 then  v1 =1 else v1=0;
>  if p >0 .6 then  v2 =1 else v2=0;
>
> Finally x will have 14 variables.
>
> Can any one show me how to do that?
>
> Thanks
> Ashta
>
>
> .
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list