An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110123/62a7f834/attachment.pl>
how to store a number into a vector
6 messages · Quan Zhou, David Winsemius, jim holtman +1 more
On Jan 23, 2011, at 1:39 PM, Quan Zhou wrote:
Hi Everyone, A quick question how to store a number like 12345 to a vector or array with size(1,5), like 1, 2, 3, 4, 5
?strsplit ?unlist
So I can compare if the last three digits of this number is the same with some other numbers.
David Winsemius, MD Heritage Laboratories West Hartford, CT
Do you want to store each digit as a separate character, or if you want to compare the last three digits with other numbers, then use the "%%" operator.
x <- 12345 x %% 1000
[1] 345
On Sun, Jan 23, 2011 at 1:39 PM, Quan Zhou <quan.poko2000 at gmail.com> wrote:
Hi Everyone, A quick question how to store a number like 12345 to a vector or array with size(1,5), like 1, 2, 3, 4, 5 So I can compare if the last three digits of this number is the same with some other numbers. Thanks a lot ? ? ? ?[[alternative HTML version deleted]]
______________________________________________ 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.
Jim Holtman Data Munger Guru What is the problem that you are trying to solve?
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110123/d210fbc1/attachment.pl>
On Jan 23, 2011, at 6:00 PM, Thomas Stewart wrote:
Here is a hack, crude solution to get the digits:
Digits <- function(a){
out <- rep(NA,nchar(a))
for(i in 1:nchar(a)) out[i] <- substr(a,i,i)
return(as.numeric(out))
}
Digits(183429)
str1 <- c(12345, 34567, 45678)
sapply(strsplit(as.character(str1), "") , unlist)
[,1] [,2] [,3]
[1,] "1" "3" "4"
[2,] "2" "4" "5"
[3,] "3" "5" "6"
[4,] "4" "6" "7"
[5,] "5" "7" "8"
--
David
If all you want is the last three numbers, consider another hack solution: Last3Digits <- function(a) as.numeric(substr(a,nchar(a)-2,nchar(a))) Last3Digits(23495329) Hope that helps. These are hack solutions, and I'm willing to bet there is better stuff available. -tgs On Sun, Jan 23, 2011 at 1:39 PM, Quan Zhou <quan.poko2000 at gmail.com> wrote:
Hi Everyone,
A quick question how to store a number like 12345 to a vector or
array with
size(1,5), like 1, 2, 3, 4, 5
So I can compare if the last three digits of this number is the
same with
some other numbers.
Thanks a lot
[[alternative HTML version deleted]]
______________________________________________ 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.
[[alternative HTML version deleted]]
______________________________________________ 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.
David Winsemius, MD Heritage Laboratories West Hartford, CT
On Jan 23, 2011, at 6:00 PM, Thomas Stewart wrote:
Here is a hack, crude solution to get the digits:
Digits <- function(a){
out <- rep(NA,nchar(a))
for(i in 1:nchar(a)) out[i] <- substr(a,i,i)
return(as.numeric(out))
}
Digits(183429)
str1 <- c(12345, 34567, 45678)
sapply(strsplit(as.character(str1), "") , unlist)
[,1] [,2] [,3]
[1,] "1" "3" "4"
[2,] "2" "4" "5"
[3,] "3" "5" "6"
[4,] "4" "6" "7"
[5,] "5" "7" "8"
-- David
If all you want is the last three numbers, consider another hack solution: Last3Digits <- function(a) as.numeric(substr(a,nchar(a)-2,nchar(a))) Last3Digits(23495329) Hope that helps. These are hack solutions, and I'm willing to bet there is better stuff available. -tgs On Sun, Jan 23, 2011 at 1:39 PM, Quan Zhou <quan.poko2000 at gmail.com> wrote:
Hi Everyone,
A quick question how to store a number like 12345 to a vector or
array with
size(1,5), like 1, 2, 3, 4, 5
So I can compare if the last three digits of this number is the
same with
some other numbers.
Thanks a lot
[[alternative HTML version deleted]]
______________________________________________ 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.
[[alternative HTML version deleted]]
______________________________________________ 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.
David Winsemius, MD Heritage Laboratories West Hartford, CT