Creat new column based on condition

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Creat new column based on condition

Sachin J
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]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Creat new column based on condition

Gabor Grothendieck
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 <[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Creat new column based on condition

Sachin J
Hi Gabor,
   
  The first one works fine. Just out of curiosity, in second solution: I dont want to create a matrix. I want to add a new column to the existing dataframe (i.e. V2 based on the values in V1). Is there a way to do it?
   
  TIA
  Sachin
   
 

Gabor Grothendieck <[hidden email]> wrote:
  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 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]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>


               
---------------------------------

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Creat new column based on condition

Gabor Grothendieck
DF <- data.frame(V1 = c(10, 20, 30, 10, 10, 20))
DF$V2 <- with(DF, 4 * (V1 == 10) + 6 * (V1 == 20) + 10 * (V1 == 30))
DF$V3 <- c(4, 6, 10)[DF$V1/10]

or

DF <- data.frame(V1 = c(10, 20, 30, 10, 10, 20))
DF <- transform(DF, V2 = 4 * (V1 == 10) + 6 * (V1 == 20) + 10 * (V1 == 30),
  V3 = c(4, 6, 10)[V1/10])

On 4/21/06, Sachin J <[hidden email]> wrote:

>
> Hi Gabor,
>
> The first one works fine. Just out of curiosity, in second solution: I dont
> want to create a matrix. I want to add a new column to the existing
> dataframe (i.e. V2 based on the values in V1). Is there a way to do it?
>
> TIA
> Sachin
>
>
>
> Gabor Grothendieck <[hidden email]> wrote:
>
> 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 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]]
> >
> > ______________________________________________
> > [hidden email] mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
> >
>
>
>
>
> ________________________________
> Love cheap thrills? Enjoy PC-to-Phone calls to 30+ countries for just 2¢/min
> with Yahoo! Messenger with Voice.
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Creat new column based on condition

Duncan Murdoch
In reply to this post by Sachin J
On 4/21/2006 4:05 PM, Sachin J 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

Gabor's solution is fine; something that looks a little bit more like
your code is this:

  V2 <- NA
  V2 <- ifelse( V1 == 10, 4, V2)
  V2 <- ifelse( V1 == 20, 6, V2)
  V2 <- ifelse( V1 == 30, 10, V2)

or

  V2 <- ifelse( V1 == 10, 4,
          ifelse( V1 == 20, 6,
            ifelse( V1 == 30, 10, NA )))

(where the NA is to handle any unexpected case where V1 isn't 10, 20 or
30).  My preference would be to use just one assignment, and if I was
sure 10, 20 and 30 were the only possibilities, would use

  V2 <- ifelse( V1 == 10, 4,
          ifelse( V1 == 20, 6, 10 ))

Duncan Murdoch

>    
>   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]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Creat new column based on condition

Peter Dalgaard
Duncan Murdoch <[hidden email]> writes:

> On 4/21/2006 4:05 PM, Sachin J 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
>
> Gabor's solution is fine; something that looks a little bit more like
> your code is this:
>
>   V2 <- NA
>   V2 <- ifelse( V1 == 10, 4, V2)
>   V2 <- ifelse( V1 == 20, 6, V2)
>   V2 <- ifelse( V1 == 30, 10, V2)
>
> or
>
>   V2 <- ifelse( V1 == 10, 4,
>           ifelse( V1 == 20, 6,
>             ifelse( V1 == 30, 10, NA )))
>
> (where the NA is to handle any unexpected case where V1 isn't 10, 20 or
> 30).  My preference would be to use just one assignment, and if I was
> sure 10, 20 and 30 were the only possibilities, would use
>
>   V2 <- ifelse( V1 == 10, 4,
>           ifelse( V1 == 20, 6, 10 ))
>
> Duncan Murdoch


 I think I'd go for something like

   V2 <- c(4, 6, 10)[factor(V1, levels=c(10, 20, 30))]


   

> >   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]]
> >
> > ______________________________________________
> > [hidden email] mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

--
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - ([hidden email])                  FAX: (+45) 35327907

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Creat new column based on condition

Frank Harrell
In reply to this post by Duncan Murdoch
Duncan Murdoch wrote:

> On 4/21/2006 4:05 PM, Sachin J 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
>
>
> Gabor's solution is fine; something that looks a little bit more like
> your code is this:
>
>   V2 <- NA
>   V2 <- ifelse( V1 == 10, 4, V2)
>   V2 <- ifelse( V1 == 20, 6, V2)
>   V2 <- ifelse( V1 == 30, 10, V2)

or

V2 <- 4*(V1==10)+6*(V2==20)+10*(V2==30)
V2[V2==0] <- NA

Frank

>
> or
>
>   V2 <- ifelse( V1 == 10, 4,
>           ifelse( V1 == 20, 6,
>             ifelse( V1 == 30, 10, NA )))
>
> (where the NA is to handle any unexpected case where V1 isn't 10, 20 or
> 30).  My preference would be to use just one assignment, and if I was
> sure 10, 20 and 30 were the only possibilities, would use
>
>   V2 <- ifelse( V1 == 10, 4,
>           ifelse( V1 == 20, 6, 10 ))
>
> Duncan Murdoch
>
>>  
>>  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

--
Frank E Harrell Jr   Professor and Chair           School of Medicine
                      Department of Biostatistics   Vanderbilt University

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Frank Harrell
Department of Biostatistics, Vanderbilt University
Reply | Threaded
Open this post in threaded view
|

Re: Creat new column based on condition

Gabor Grothendieck
In reply to this post by Gabor Grothendieck
Here is a compact solution using approx:

  DF$V2 <- approx(c(10, 20, 30), c(4,6,10), DF$V1)$y


On 4/21/06, Gabor Grothendieck <[hidden email]> wrote:

> DF <- data.frame(V1 = c(10, 20, 30, 10, 10, 20))
> DF$V2 <- with(DF, 4 * (V1 == 10) + 6 * (V1 == 20) + 10 * (V1 == 30))
> DF$V3 <- c(4, 6, 10)[DF$V1/10]
>
> or
>
> DF <- data.frame(V1 = c(10, 20, 30, 10, 10, 20))
> DF <- transform(DF, V2 = 4 * (V1 == 10) + 6 * (V1 == 20) + 10 * (V1 == 30),
>  V3 = c(4, 6, 10)[V1/10])
>
> On 4/21/06, Sachin J <[hidden email]> wrote:
> >
> > Hi Gabor,
> >
> > The first one works fine. Just out of curiosity, in second solution: I dont
> > want to create a matrix. I want to add a new column to the existing
> > dataframe (i.e. V2 based on the values in V1). Is there a way to do it?
> >
> > TIA
> > Sachin
> >
> >
> >
> > Gabor Grothendieck <[hidden email]> wrote:
> >
> > 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 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]]
> > >
> > > ______________________________________________
> > > [hidden email] mailing list
> > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > PLEASE do read the posting guide!
> > http://www.R-project.org/posting-guide.html
> > >
> >
> >
> >
> >
> > ________________________________
> > Love cheap thrills? Enjoy PC-to-Phone calls to 30+ countries for just 2¢/min
> > with Yahoo! Messenger with Voice.
> >
> >
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Creat new column based on condition

Peter Dalgaard
In reply to this post by Peter Dalgaard
Peter Dalgaard <[hidden email]> writes:

>  I think I'd go for something like
>
>    V2 <- c(4, 6, 10)[factor(V1, levels=c(10, 20, 30))]

Come to think of it:

   c(4, 6, 10)[match(V1, c(10, 20, 30))]

is more to the point.


--
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - ([hidden email])                  FAX: (+45) 35327907

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html