Jessica Cathro
2010-Mar-26 10:37 UTC
[R] Help with assigning a value based on existing numbers
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@london.edu <mailto:jcathro@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]]
Petr PIKAL
2010-Mar-26 13:59 UTC
[R] Odp: Help with assigning a value based on existing numbers
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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
An expression like "v >= 52", where v is a vector, will produce a vector resulting from comparing each entry -- that is why you see the message. What you want to do is logical subscripting. For example names <- character( nrow( curveData ) ) names[ curvedata$Date.difference <= 29 ] = "< 1 month" etc. Also, to test for a NULL value do not compare to the string "NULL", use the test operator is.null: names[ is.null( curvedata$Date.difference ) ] = "missing" -- View this message in context: http://n4.nabble.com/Help-with-assigning-a-value-based-on-existing-numbers-tp1692200p1692321.html Sent from the R help mailing list archive at Nabble.com.
Petr PIKAL
2010-Mar-29 05:55 UTC
[R] Help with assigning a value based on existing numbers
Hi "Jessica Cathro" <jcathro at london.edu> napsal dne 26.03.2010 15:33:19:> Hi > Can you explain what the purpose of the cut function is, and how I would > use it??cut x<-runif(100)*300 cut(x, breaks=seq(0,300,50), labels=letters[1:6]) Regards Petr> 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 > > www.london.edu | London experience. World impact. > -----Original Message----- > From: Petr PIKAL [mailto:petr.pikal at precheza.cz] > Sent: 26 March 2010 14:00 > To: Jessica Cathro > Cc: r-help at r-project.org > Subject: Odp: [R] Help with assigning a value based on existing numbers > > 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. > > > ______________________________________________________________________ > > 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 > ______________________________________________________________________ > > ______________________________________________________________________ > > 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 > ______________________________________________________________________