An embedded and charset-unspecified text was scrubbed... Name: not available Url: https://stat.ethz.ch/pipermail/r-help/attachments/20051020/ffdc43b5/attachment.pl
spliting an integer
7 messages · Dimitri Szerman, Anne Hertel, Sundar Dorai-Raj +4 more
Hi Dimitri, You could write
z <- trunc(x/10000) z
[1] 1 12 8
y <- x-trunc(x/10000)*10000 y
[1] 1999 2000 1997 And there you have it. Cheers, Anne Hertel On Thu, 20 Oct 2005 17:40:10 -0200
"Dimitri Szerman" <dimitrijoe at 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". Any suggestions? Thanks in advance, Dimitri [[alternative HTML version deleted]]
______________________________________________ R-help at 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
------------------------------------------------------------ Anne M. K. Hertel Grad. Student & Research Assistant Department of Atmospheric Sciences University of Illinois at Urbana-Champaign Annex II, room 204 Phone: (217) 333 6296
Dimitri Szerman 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". Any suggestions? Thanks in advance, Dimitri [[alternative HTML version deleted]]
Try: X <- c(11999, 122000, 81997) Y <- X %/% 10000 Z <- X - Y * 10000 See ?Arithmetic for more details. HTH, --sundar
On 10/20/05, Dimitri Szerman <dimitrijoe at 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))
Hint: 11999 = 11999%%1e4 + 1e4*(11999%/%1e4) -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA "The business of the statistician is to catalyze the scientific learning process." - George E. P. Box
-----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Dimitri Szerman Sent: Thursday, October 20, 2005 12:40 PM To: R-Help Subject: [R] spliting an integer 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". Any suggestions? Thanks in advance, Dimitri [[alternative HTML version deleted]]
______________________________________________ R-help at 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
"Anne Hertel" <ahertel at atmos.uiuc.edu> writes:
Hi Dimitri, You could write
z <- trunc(x/10000) z
[1] 1 12 8
y <- x-trunc(x/10000)*10000 y
[1] 1999 2000 1997 And there you have it.
Er, we do have integer divide and remainder operators:
X = c(11999, 122000, 81997) X %% 1e4
[1] 1999 2000 1997
X %/% 1e4
[1] 1 12 8
Cheers, Anne Hertel On Thu, 20 Oct 2005 17:40:10 -0200 "Dimitri Szerman" <dimitrijoe at 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". Any suggestions? Thanks in advance, Dimitri [[alternative HTML version deleted]]
______________________________________________ R-help at 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
------------------------------------------------------------ Anne M. K. Hertel Grad. Student & Research Assistant Department of Atmospheric Sciences University of Illinois at Urbana-Champaign Annex II, room 204 Phone: (217) 333 6296
______________________________________________ R-help at 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
O__ ---- Peter Dalgaard ??ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Dear Sundar and Dimitri,
-----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Sundar Dorai-Raj Sent: Thursday, October 20, 2005 3:50 PM To: Dimitri Szerman Cc: R-Help Subject: Re: [R] spliting an integer Dimitri Szerman 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".
Any suggestions? Thanks in advance, Dimitri [[alternative HTML version deleted]]
Try: X <- c(11999, 122000, 81997) Y <- X %/% 10000 Z <- X - Y * 10000
Or even
X %% 10000
[1] 1999 2000 1997 Regards, John
See ?Arithmetic for more details. HTH, --sundar
______________________________________________ R-help at 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