Re: [R] spliting an integer

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Fri 21 Oct 2005 - 07:24:05 EST

On 10/20/05, Dimitri Szerman <dimitrijoe@ipea.gov.br> wrote:
> Hi there,
>
> >From the vector X of integers,
>
> X = c(11999, 122000, 81997)
>
> I would like to make these two vectors:
>
> Z= c(1999, 2000, 1997)
> Y =c(1 , 12 , 8)
>
> That is, each entry of vector Z receives the four last digits of each entry of X, and Y receives "the rest".
>

Some possibilities:

  1. Use integer division and remainder (probably best solution):
	Y <- X %/% 10000
	Z <- X %% 10000

2. Convert to character and reduce to desired field:

	Y <- as.numeric(sub("....$", "", X))
	Z <- as.numeric(sub(".*(....)$", "\\1", X))

3. Insert a space between the two sets and read them in:

	read.table(textConnection(sub("(....)$", " \\1", X)),
		col.names = c("Y", "Z"))

4. Use encode at:

http://www.wiwi.uni-bielefeld.de/~wolf/software/R-wtools/decodeencode/decodeencode.rev

        encode(X, c(100, 10000))



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Fri Oct 21 08:32:14 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 19:14:45 EST