[R] Creating new variables

David Winsemius dwinsemius at comcast.net
Sun Oct 11 03:23:12 CEST 2009


On Oct 10, 2009, at 7:49 PM, Ashta wrote:

> Thanks. This helps. How do I generate P?
> Will this work?
>
> p1<-pnorm(mean=0, std=1)
> p2<-pnorm(mean=0, std=1)
>
Seems unlikely. You are not supplying sufficient arguments for the  
pnorm function for one thing. For another I suspect you really want  
the rnorm function and the first argument to it is the number of  
random numbers to return:
Perhaps
p1<-rnorm(200. mean=0, std=1)
p2<-pnorm(200, mean=0, std=1)

> x <- cbind(x, v1=ifelse(x[,'p'] > 0.4, 1, 0), v2=ifelse(x[,'2'] >  
> 0.6, 0,
> 1))
>
Jim's suggestion will work but even simpler would be:

x <- cbind(x, v1 = (p1>0.4)+0, v2 = (p2> 0.6)+0 )

>
>
>
>
> 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))
>
>
> On Sat, Oct 10, 2009 at 6:32 PM, jim holtman <jholtman at gmail.com>  
> wrote:
>
>> 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?
>>
>
> 	[[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.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list