Hello all, Frustrated and i know you can help I need to drop the last numeral of each of my values in my data set. So for the following i have tried the ?substring but since i have to specify the length, but because my data are of varying lengths it doenst work so well Data<-c("1131", "1132", "1731" ,"1732" ,"1821" ,"1822", "2221" ,"2222", "2241" ,"2242","414342" ,"414371" ,"414372") Bldgid<-substring(as.character(Data),1,3) returns: "113" "113" "173" "173" "182" "182" "222" "222" "224" "224" "414" "414" "414" but i want" "113", "113", "173" ,"173" ,"182" ,"182", "222" ,"222", "224" ,"224","41434" ,"41437" ,"41437") The values thats have more than 4 numerals are whats messing things up. Tried ?formatC as well but couldn't get it to coerce things correctly. Thanks for the help JR -- View this message in context: http://n4.nabble.com/Drop-last-numeral-tp1012347p1012347.html Sent from the R help mailing list archive at Nabble.com.
Data<-c("1131", "1132", "1731" ,"1732" ,"1821" ,"1822", "2221" ,"2222", "2241" ,"2242","414342" ,"414371" ,"414372") Bldgid<-substring(as.character(Data),1,nchar(Data)-1) -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of LCOG1 Sent: Tuesday, January 12, 2010 1:37 PM To: r-help at r-project.org Subject: [R] Drop last numeral Hello all, Frustrated and i know you can help I need to drop the last numeral of each of my values in my data set. So for the following i have tried the ?substring but since i have to specify the length, but because my data are of varying lengths it doenst work so well Data<-c("1131", "1132", "1731" ,"1732" ,"1821" ,"1822", "2221" ,"2222", "2241" ,"2242","414342" ,"414371" ,"414372") Bldgid<-substring(as.character(Data),1,3) returns: "113" "113" "173" "173" "182" "182" "222" "222" "224" "224" "414" "414" "414" but i want" "113", "113", "173" ,"173" ,"182" ,"182", "222" ,"222", "224" ,"224","41434" ,"41437" ,"41437") The values thats have more than 4 numerals are whats messing things up. Tried ?formatC as well but couldn't get it to coerce things correctly. Thanks for the help JR -- View this message in context: http://n4.nabble.com/Drop-last-numeral-tp1012347p1012347.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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. ================================== P Please consider the environment before printing this e-mail Cleveland Clinic is ranked one of the top hospitals in America by U.S.News & World Report (2009). Visit us online at http://www.clevelandclinic.org for a complete listing of our services, staff and locations. Confidentiality Note: This message is intended for use\...{{dropped:13}}
Also try sub('[0-9]$', '', Data) [1] "113" "113" "173" "173" "182" "182" "222" "222" "224" [10] "224" "41434" "41437" "41437" HTH, Dennis On Tue, Jan 12, 2010 at 10:36 AM, LCOG1 <jroll@lcog.org> wrote:> > Hello all, > Frustrated and i know you can help > > I need to drop the last numeral of each of my values in my data set. So > for > the following i have tried the ?substring but since i have to specify the > length, but because my data are of varying lengths it doenst work so well > > Data<-c("1131", "1132", "1731" ,"1732" ,"1821" ,"1822", "2221" ,"2222", > "2241" ,"2242","414342" ,"414371" ,"414372") > Bldgid<-substring(as.character(Data),1,3) > > returns: > "113" "113" "173" "173" "182" "182" "222" "222" "224" "224" "414" "414" > "414" > > but i want" > > "113", "113", "173" ,"173" ,"182" ,"182", "222" ,"222", "224" > ,"224","41434" > ,"41437" ,"41437") > > The values thats have more than 4 numerals are whats messing things up. > Tried ?formatC as well but couldn't get it to coerce things correctly. > Thanks for the help > > JR > -- > View this message in context: > http://n4.nabble.com/Drop-last-numeral-tp1012347p1012347.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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]]
Try this: substr(Data,1,nchar(Data)-1) Steve>>>From: LCOG1 <jroll@lcog.org> To:<r-help@r-project.org> Date: 13/Jan/2010 9:15 a.m. Subject: [R] Drop last numeral Hello all, Frustrated and i know you can help I need to drop the last numeral of each of my values in my data set. So for the following i have tried the ?substring but since i have to specify the length, but because my data are of varying lengths it doenst work so well Data<-c("1131", "1132", "1731" ,"1732" ,"1821" ,"1822", "2221" ,"2222", "2241" ,"2242","414342" ,"414371" ,"414372") Bldgid<-substring(as.character(Data),1,3) returns: "113" "113" "173" "173" "182" "182" "222" "222" "224" "224" "414" "414" "414" but i want" "113", "113", "173" ,"173" ,"182" ,"182", "222" ,"222", "224" ,"224","41434" ,"41437" ,"41437") The values thats have more than 4 numerals are whats messing things up. Tried ?formatC as well but couldn't get it to coerce things correctly. Thanks for the help JR -- View this message in context: http://n4.nabble.com/Drop-last-numeral-tp1012347p1012347.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R ( http://www.r/ )-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]]
In addition to the substring and regular expression solutions, if you are certain that everything will be numeric (and integer as in your examples), then you could just convert to numeric, divide by 10, and then drop the decimal (floor or as.integer). -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of LCOG1 > Sent: Tuesday, January 12, 2010 11:37 AM > To: r-help at r-project.org > Subject: [R] Drop last numeral > > > Hello all, > Frustrated and i know you can help > > I need to drop the last numeral of each of my values in my data set. > So for > the following i have tried the ?substring but since i have to specify > the > length, but because my data are of varying lengths it doenst work so > well > > Data<-c("1131", "1132", "1731" ,"1732" ,"1821" ,"1822", "2221" ,"2222", > "2241" ,"2242","414342" ,"414371" ,"414372") > Bldgid<-substring(as.character(Data),1,3) > > returns: > "113" "113" "173" "173" "182" "182" "222" "222" "224" "224" "414" "414" > "414" > > but i want" > > "113", "113", "173" ,"173" ,"182" ,"182", "222" ,"222", "224" > ,"224","41434" > ,"41437" ,"41437") > > The values thats have more than 4 numerals are whats messing things up. > Tried ?formatC as well but couldn't get it to coerce things correctly. > Thanks for the help > > JR > -- > View this message in context: http://n4.nabble.com/Drop-last-numeral- > tp1012347p1012347.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.
The Below worked best for my purposes. Thanks everyone. Data<-c("1131", "1132", "1731" ,"1732" ,"1821" ,"1822", "2221" ,"2222", "2241" ,"2242","414342" ,"414371" ,"414372") substr(Data,1,nchar(Data)-1) LCOG1 wrote:> > Hello all, > Frustrated and i know you can help > > I need to drop the last numeral of each of my values in my data set. So > for the following i have tried the ?substring but since i have to specify > the length, but because my data are of varying lengths it doenst work so > well > > Data<-c("1131", "1132", "1731" ,"1732" ,"1821" ,"1822", "2221" ,"2222", > "2241" ,"2242","414342" ,"414371" ,"414372") > Bldgid<-substring(as.character(Data),1,3) > > returns: > "113" "113" "173" "173" "182" "182" "222" "222" "224" "224" "414" "414" > "414" > > but i want" > > "113", "113", "173" ,"173" ,"182" ,"182", "222" ,"222", "224" > ,"224","41434" ,"41437" ,"41437") > > The values thats have more than 4 numerals are whats messing things up. > Tried ?formatC as well but couldn't get it to coerce things correctly. > Thanks for the help > > JR >-- View this message in context: http://n4.nabble.com/Drop-last-numeral-tp1012347p1012492.html Sent from the R help mailing list archive at Nabble.com.