Thank you David; this seems to perform the required task with relative ease, which is all I could ask for at the moment!
Tyler On Sat, Nov 13, 2010 at 1:32 PM, David Winsemius <dwinsem...@comcast.net>wrote: > > On Nov 13, 2010, at 12:34 PM, T.D. Rudolph wrote: > > >> Is there any really easy way to truncate integers with several consecutive >> digits without rounding and without converting from numeric to character >> (using strsplit, etc.)?? Something along these lines: >> >> e.g. = 456 >> >> truncfun(e.g., location=1) >> = 4 >> >> truncfun(e.g., location=1:2) >> = 45 >> >> truncfun(e.g., location=2:3) >> = 56 >> >> truncfun(e.g., location=3) >> = 6 >> >> It's one thing using floor(x/100) to get 4 or floor(x/10) to get 45, but >> I'd >> like to return only 5 or only 6, for example, in cases where I don't know >> what the numbers are going to be. >> >> I'm sure there is something very logical that I am missing, but my code is >> getting too complicated and I can't seem to find a parsimonious solution. >> > > Modulo arithmetic? (not the same location arguments as you specified but > should give you ideas to work with: > > trncint <- function(x, left=0,length=0) (x %% 10^(left) ) %/% > 10^(left-length) > > > trncint(456, 2,2) > [1] 56 > > trncint(456, 3,2) > [1] 45 > > trncint(456, 3,1) > [1] 4 > > trncint(456, 2,1) > [1] 5 > > trncint(456, 3,1) > [1] 4 > > > trncint(456, 1,1) > [1] 6 > > -- > > David Winsemius, MD > West Hartford, CT > > [[alternative HTML version deleted]] ______________________________________________ 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.