[R] Odp: Help with assigning a value based on existing numbers

Petr PIKAL petr.pikal at precheza.cz
Fri Mar 26 14:59:36 CET 2010


Hi

again a work for cut.

See ?cut and notice a labels option.

Regards
Petr

r-help-bounces at r-project.org napsal dne 26.03.2010 11:37:20:

> Hi All
> 
> I have a column/variable called time difference.  It has a whole list of
> numbers from 0 through to the hundreds eg 236.  I want to assign a
> corresponding "name" to each variable from a predefined list: Month or
> less, 1 -2 months, 2-3 months etc
> 
> So the result would look something like:
> 
> Time Difference               Month
> 
> 1                                              Month or less
> 
> 365                                         1-2 years
> 
> 52                                           2-3 months
> 
> Etc
> 
> 
> 
> I have tried using if elseif (as shown below), but it kept coming up
> with the error:
> 
> Error:
> 
> In if (curvedata$Date.difference == "NULL") { :
> 
>   the condition has length > 1 and only the first element will be used
> 
> 2: In if (curvedata$Date.difference <= 29) { :
> 
>   the condition has length > 1 and only the first element will be used
> 
> 3: In if (curvedata$Date.difference <= 59) { :
> 
>   the condition has length > 1 and only the first element will be used
> 
> 
> 
> If Else Statement:
> 
> month <-if (curvedata$Date.difference =="NULL") {Month <-0}  ifelse
> 
> (curvedata$Date.difference <=29) {Month <-"month or less"} else if
> 
> (curvedata$Date.difference <=59) {Month <-"1-2 months"} else if
> 
> (curvedata$Date.difference <=89) {Month <-"2-3 months"} else if
> 
> (curvedata$Date.difference <=119) {Month <-"3-4 months"} else if
> 
> (curvedata$Date.difference <=149) {Month <-"4-5 months"} else if
> 
> (curvedata$Date.difference <=179) {Month <-"5-6 months"} else if
> 
> (curvedata$Date.difference <=209) {Month <-"6-7 months"} else if
> 
> (curvedata$Date.difference <=239) {Month <-"7-8 months"} else if
> 
> (curvedata$Date.difference <=269) {Month <-"8-9 months"} else if
> 
> (curvedata$Date.difference <=299) {Month <-"9-10 months"} else if
> 
> (curvedata$Date.difference <=329) {Month <-"10-11 months"} else if
> 
> (curvedata$Date.difference <=359) {Month <-"11-12 months"} else if
> 
> (curvedata$Date.difference <=719) {Month <-"1-2 years"} else 
> 
> {Month <-"More than 2 years"}
> 
> 
> 
> I'm pretty new to R, so any help would be appreciated.
> 
> Thanks
> 
> Jessica
> 
> _________________________________
> 
> Jessica Cathro | Business Analyst | Executive Education 
> London Business School | Regent's Park | London NW1 4SA | United Kingdom
> Switchboard +44 (0)20 7000 7000| Direct line +44 (0)20 7000 7383 |
> Email jcathro at london.edu <mailto:jcathro at london.edu> 
> 
> www.london.edu <http://www.london.edu/>  | London experience. World
> impact.
> 
> 
> ______________________________________________________________________
> 
> This email has been scanned by the MessageLabs Email Security System
> on behalf of the London Business School community.
> For more information please visit http://www.messagelabs.com/email
> ______________________________________________________________________
>    [[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.



More information about the R-help mailing list