Goal: Suppose you have a vector that is a discrete variable with values ranging from 1 to 3, and length of 10. We'll use this as the example: y <- c(1,2,3,1,2,3,1,2,3,1) ...and suppose you want your new vector (y.new) to be equal in length to the possible discrete values (3) times the length (10), and formatted in such a way that if y[1] == 1, then y.new[1:3] == c(1,0,0), and if y[2] == 2, then y.new[4:6] == c(0,1,0). For example, the final goal should be: y.new <- c(1,0,0,0,1,0,0,0,1,1,0,0,0,1,0,0,0,1,1,0,0,0,1,0,0,0,1,1,0,0) Note: I know how to do this with loops, but that's not taking advantage of R's capabilities with vectors and, I suspect, matrices. So far, here's my best: y <- c(1,2,3,1,2,3,1,2,3,1) y.k <- length(unique(y)) #Num. of Categories of y y.n <- NROW(y) y.nk <- y.n * y.k #Length of new vector y.1 <- ifelse(y == 1,1,0) y.2 <- ifelse(y == 2,1,0) y.3 <- ifelse(y == 3,1,0) z <- cbind(y.1, y.2, y.3) z.trans <- t(z) y.new <- c(1:y.nk); y.new[1:y.nk] <- NA y.new <- as.vector(z.trans) rm(y.k, y.n, y.nk, y.1, y.2, y.3, z, z.trans) y y.new Is there a better a way? I'm still pretty new. Thanks. -- View this message in context: http://www.nabble.com/What%27s-the-BEST-way-in-R-to-adapt-this-vector--tp20638991p20638991.html Sent from the R help mailing list archive at Nabble.com.
On Sat, Nov 22, 2008 at 12:00 PM, zerfetzen <zerfetzen at yahoo.com> wrote:> > Goal: > Suppose you have a vector that is a discrete variable with values ranging > from 1 to 3, and length of 10. We'll use this as the example: > > y <- c(1,2,3,1,2,3,1,2,3,1) > > ...and suppose you want your new vector (y.new) to be equal in length to the > possible discrete values (3) times the length (10), and formatted in such a > way that if y[1] == 1, then y.new[1:3] == c(1,0,0), and if y[2] == 2, then > y.new[4:6] == c(0,1,0). For example, the final goal should be: > > y.new <- c(1,0,0,0,1,0,0,0,1,1,0,0,0,1,0,0,0,1,1,0,0,0,1,0,0,0,1,1,0,0) > > Note: I know how to do this with loops, but that's not taking advantage of > R's capabilities with vectors and, I suspect, matrices.How about: as.vector(diag(3)[, y]) Hadley -- http://had.co.nz/
Gabor Grothendieck
2008-Nov-23 03:12 UTC
[R] What's the BEST way in R to adapt this vector?
Try this: outer(y, sort(unique(y)), "==")+0 On Sat, Nov 22, 2008 at 3:37 PM, zerfetzen <zerfetzen at yahoo.com> wrote:> > Goal: > Suppose you have a vector that is a discrete variable with values ranging > from 1 to 3, and length of 10. We'll use this as the example: > > y <- c(1,2,3,1,2,3,1,2,3,1) > > ...and suppose you want your new vector (y.new) to be equal in length to the > possible discrete values (3) times the length (10), and formatted in such a > way that if y[1] == 1, then y.new[1:3] == c(1,0,0), and if y[2] == 2, then > y.new[4:6] == c(0,1,0). For example, the final goal should be: > > y.new <- c(1,0,0,0,1,0,0,0,1,1,0,0,0,1,0,0,0,1,1,0,0,0,1,0,0,0,1,1,0,0) > > Note: I know how to do this with loops, but that's not taking advantage of > R's capabilities with vectors and, I suspect, matrices. > > So far, here's my best: > > y <- c(1,2,3,1,2,3,1,2,3,1) > y.k <- length(unique(y)) #Num. of Categories of y > y.n <- NROW(y) > y.nk <- y.n * y.k #Length of new vector > y.1 <- ifelse(y == 1,1,0) > y.2 <- ifelse(y == 2,1,0) > y.3 <- ifelse(y == 3,1,0) > z <- cbind(y.1, y.2, y.3) > z.trans <- t(z) > y.new <- c(1:y.nk); y.new[1:y.nk] <- NA > y.new <- as.vector(z.trans) > rm(y.k, y.n, y.nk, y.1, y.2, y.3, z, z.trans) > y > y.new > > Is there a better a way? I'm still pretty new. Thanks. > -- > View this message in context: http://www.nabble.com/What%27s-the-BEST-way-in-R-to-adapt-this-vector--tp20638991p20638991.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >
On Sat, 22 Nov 2008 10:00:18 -0800 (PST) zerfetzen <zerfetzen at yahoo.com> wrote:> Goal: > Suppose you have a vector that is a discrete variable with values > ranging from 1 to 3, and length of 10. We'll use this as the example: > > y <- c(1,2,3,1,2,3,1,2,3,1) > > ...and suppose you want your new vector (y.new) to be equal in length > to the possible discrete values (3) times the length (10), and > formatted in such a way that if y[1] == 1, then y.new[1:3] => c(1,0,0), and if y[2] == 2, then y.new[4:6] == c(0,1,0). For > example, the final goal should be: > > y.new <- > c(1,0,0,0,1,0,0,0,1,1,0,0,0,1,0,0,0,1,1,0,0,0,1,0,0,0,1,1,0,0) > > Note: I know how to do this with loops, but that's not taking > advantage of R's capabilities with vectors and, I suspect, matrices.> So far, my best guess would be to start as follows:[....] My guess would be to use: R> y <- c(1,2,3,1,2,3,1,2,3,1) R> y.new <- as.numeric(as.vector(outer(sort(unique(y)), y, "=="))) R> y.new [1] 1 0 0 0 1 0 0 0 1 1 0 0 0 1 0 0 0 1 1 0 0 0 1 0 0 0 1 1 0 0> [...] From here, maybe put these into a 10x3 matrix, and read them out > by row into y.new?Better put them into a 3x10 matrix and then turn the matrix into a vector by as.vector(). Essentially, a matrix is a vector with an attribute giving the dimensions of a matrix and as.vector() removes that attribute. Furthermore, in R (just as in Fortran), matrices are stored in column major format; thus, as soon as the dim attribute is removed, the values are in the correct order. HTH. Cheers, Berwin =========================== Full address ============================Berwin A Turlach Tel.: +65 6516 4416 (secr) Dept of Statistics and Applied Probability +65 6516 6650 (self) Faculty of Science FAX : +65 6872 3919 National University of Singapore 6 Science Drive 2, Blk S16, Level 7 e-mail: statba at nus.edu.sg Singapore 117546 http://www.stat.nus.edu.sg/~statba
Thanks for the help, I've a few more functions to get familiar with. Those are definitely concise ways to do this! :) -- View this message in context: http://www.nabble.com/What%27s-the-BEST-way-in-R-to-adapt-this-vector--tp20638991p20647978.html Sent from the R help mailing list archive at Nabble.com.
Jagat.K.Sheth at wellsfargo.com
2008-Nov-24 02:00 UTC
[R] What's the BEST way in R to adapt this vector?
bEST is up to you to define. Here is one simple way y.new <- c(t(model.matrix(~factor(y)-1))) -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of zerfetzen Sent: Saturday, November 22, 2008 12:00 PM To: r-help at r-project.org Subject: [R] What's the BEST way in R to adapt this vector? Goal: Suppose you have a vector that is a discrete variable with values ranging from 1 to 3, and length of 10. We'll use this as the example: y <- c(1,2,3,1,2,3,1,2,3,1) ...and suppose you want your new vector (y.new) to be equal in length to the possible discrete values (3) times the length (10), and formatted in such a way that if y[1] == 1, then y.new[1:3] == c(1,0,0), and if y[2] == 2, then y.new[4:6] == c(0,1,0). For example, the final goal should be: y.new <- c(1,0,0,0,1,0,0,0,1,1,0,0,0,1,0,0,0,1,1,0,0,0,1,0,0,0,1,1,0,0) Note: I know how to do this with loops, but that's not taking advantage of R's capabilities with vectors and, I suspect, matrices. So far, my best guess would be to start as follows: y1 <- ifelse(y == 1, 1, 0) y2 <- ifelse(y == 2, 1, 0) y3 <- ifelse(y == 3, 1, 0)>From here, maybe put these into a 10x3 matrix, and read them out by row>intoy.new? Is that even the most efficient way? If it is, I'm sure I can get them into a matrix, but how do I read them out correctly? Thanks for any input. -- View this message in context: http://www.nabble.com/What%27s-the-BEST-way-in-R-to-adapt-this-vector--t p20638991p20638991.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.
Possibly Parallel Threads
- Equivalent to a BY command in SAS
- Legality Question about R's Open Source GNU GPL License
- [PATCH 3/3, take 2] lib: Add support for creating nodes (keys) and values with UTF-16LE-encoded names
- Re: [PATCH 3/3] lib: Add support for creating nodes (keys) and values with UTF-16LE-encoded names
- outer function problems