[R] if else elseif for data frames

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Mon Aug 13 04:38:37 CEST 2012


Maybe the cut function?

?cut
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

jim holtman <jholtman at gmail.com> wrote:

>You might look at the 'recode' that is part of the 'car' package.  You
>can also setup a dataframe with two columns; one with the current
>value and one with the new value you want, then you can use 'merge' or
>'match' to create your new column.  If you have provided some sample
>data, I could have provided examples, but now left as an exercise for
>the reader.
>
>On Sun, Aug 12, 2012 at 9:07 PM, Sachinthaka Abeywardana
><sachin.abeywardana at gmail.com> wrote:
>> The thing is I have about 10 cases. I saw the ifelse statement but
>was
>> wondering if there was a cleaner method of doing it. The coding will
>get
>> really messy when I write all 10 cases.
>>
>> Cheers,
>> Sachin
>>
>> On Mon, Aug 13, 2012 at 11:04 AM, arun <smartpink111 at yahoo.com>
>wrote:
>>
>>> Hi,
>>> Try this:
>>>
>dat1<-data.frame(col1=c(rep("high",3),rep("Neutral",3),rep("low",4)))
>>>
>>> 
>dat1$col2<-ifelse(dat1$col1=="high",dat1$col2<-"H",ifelse(dat1$col1=="Neutral",dat1$col2<-"N","L"))
>>> dat1
>>>       col1 col2
>>> 1     high    H
>>> 2     high    H
>>> 3     high    H
>>> 4  Neutral    N
>>> 5  Neutral    N
>>> 6  Neutral    N
>>> 7      low    L
>>> 8      low    L
>>> 9      low    L
>>> 10     low    L
>>>
>>> A.K.
>>>
>>>
>>>
>>>
>>> ----- Original Message -----
>>> From: Sachinthaka Abeywardana <sachin.abeywardana at gmail.com>
>>> To: r-help at r-project.org
>>> Cc:
>>> Sent: Sunday, August 12, 2012 8:43 PM
>>> Subject: [R] if else elseif for data frames
>>>
>>> Hi all,
>>>
>>> It seems like I cannot use normal 'if' for data frames. What would
>be the
>>> best way to do the following.
>>>
>>> if data$col1='high'
>>>     data$col2='H'
>>> else if data$col1='Neutral'
>>>     data$col2='N'
>>> else if data$col='low'
>>>    data$col2='L'
>>> else
>>>    #chuch a warning?
>>>
>>>
>>> Note that col2 was not an existing column and was newly assigned for
>this
>>> task.
>>>
>>> Thanks,
>>> Sachin
>>>
>>>     [[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.
>>>
>>>
>>
>>         [[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
>Data Munger Guru
>
>What is the problem that you are trying to solve?
>Tell me what you want to do, not how you want to do it.
>
>______________________________________________
>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.



More information about the R-help mailing list