Skip to content
Prev 79439 / 398503 Next

spliting an integer

On 10/20/05, Dimitri Szerman <dimitrijoe at ipea.gov.br> wrote:
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))