Dear list members: I am looking for an elegant (or efficient) way to accomplish the following: take a large boolean vector and fill the TRUE values with the values from a smaller boolean vector that has a length that is the number of TRUE values of the large vector. Example: large<- c(FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE, FALSE) small<- c(TRUE, FALSE, TRUE) desired output = c(FALSE, FALSE, FALSE, *TRUE*, FALSE, FALSE, *FALSE*, FALSE, FALSE, FALSE, *TRUE*, FALSE) (without the asterisks! ) my first thought as someone new to R was ifelse(large,small, large) but that returns: c(FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE) because small is cycled to match the size of large instead of the size of the TRUE subset of large. I am guessing that there is probably a way to do this without writing a loop, but I just don't know the syntax. -mph
Wacek Kusnierczyk
2008-Dec-24 09:22 UTC
[R] filling values in a vector using smaller vector
Milton Huang wrote:> Dear list members: > > I am looking for an elegant (or efficient) way to accomplish the following: > > take a large boolean vector and fill the TRUE values with the values from a > smaller boolean vector that has a length that is the number of TRUE values of > the large vector. > > Example: > > large<- c(FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, > TRUE, FALSE) > > small<- c(TRUE, FALSE, TRUE) > > desired output = c(FALSE, FALSE, FALSE, *TRUE*, FALSE, FALSE, *FALSE*, FALSE, > FALSE, FALSE, *TRUE*, FALSE) > >large[which(large)] = small # large[which(large)] = paste("*", small, "*", sep="") to see it's as you specify ?which vQ
I believe the following does what is wanted: desired <- large desired[large] <- small Patrick Burns patrick at burns-stat.com +44 (0)20 8525 0696 http://www.burns-stat.com (home of S Poetry and "A Guide for the Unwilling S User") Milton Huang wrote:> Dear list members: > > I am looking for an elegant (or efficient) way to accomplish the following: > > take a large boolean vector and fill the TRUE values with the values from a > smaller boolean vector that has a length that is the number of TRUE values of > the large vector. > > Example: > > large<- c(FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, > TRUE, FALSE) > > small<- c(TRUE, FALSE, TRUE) > > desired output = c(FALSE, FALSE, FALSE, *TRUE*, FALSE, FALSE, *FALSE*, FALSE, > FALSE, FALSE, *TRUE*, FALSE) > > (without the asterisks! ) > > my first thought as someone new to R was > ifelse(large,small, large) > > but that returns: c(FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE > FALSE FALSE FALSE) because small is cycled to match the size of large instead > of the size of the TRUE subset of large. > > I am guessing that there is probably a way to do this without writing a loop, > but I just don't know the syntax. > > -mph > > ______________________________________________ > 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. > > >
Thank you both for such beautiful solutions. Just what I was looking for! I love the Internet, R, and the R-list! There is so much opportunity to learn. In fact, looking at the replace function, I see the two solutions are the same:> replacefunction (x, list, values) { x[list] <- values x } <environment: namespace:base> Thanks again. You made my day. Have a happy holiday season. -milton =====================On Wednesday 24 December 2008 1:46 am, you wrote:> Wacek Kusnierczyk wrote: > > Milton Huang wrote: > >>> Dear list members: > >>> > >>> I am looking for an elegant (or efficient) way to accomplish the > >>> following: > >>> > >>> take a large boolean vector and fill the TRUE values with the values > >>> from a smaller boolean vector that has a length that is the number of > >>> TRUE values of the large vector. > >>> > >>> Example: > >>> > >>> large<- c(FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, > >>> FALSE, TRUE, FALSE) > >>> > >>> small<- c(TRUE, FALSE, TRUE) > >>> > >>> desired output = c(FALSE, FALSE, FALSE, *TRUE*, FALSE, FALSE, *FALSE*, > >>> FALSE, FALSE, FALSE, *TRUE*, FALSE) > > > > replace(large, which(large), small) > > in fact, this will do: > > replace(large, large, small) > > vQ-------------------------- I believe the following does what is wanted: desired <- large desired[large] <- small Patrick Burns patrick at burns-stat.com +44 (0)20 8525 0696 http://www.burns-stat.com (home of S Poetry and "A Guide for the Unwilling S User")