[R] Drop last numeral

Greg Snow Greg.Snow at imail.org
Tue Jan 12 21:21:17 CET 2010


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.



More information about the R-help mailing list