Hi, I am looking for a one-liner code for the following situation say I have a vector r of 0 and 1's, and another vector v of equal length. I want to assign v[i]-1 to v[i] if r[i]=1, for all i. How would I do that using mapply or any other batch processing keywords? thanks. John -- View this message in context: http://r.789695.n4.nabble.com/element-wise-conditional-assignment-in-r-tp2951963p2951963.html Sent from the R help mailing list archive at Nabble.com.
John -
Is this what you're looking for?
> r = sample(c(0,1),10,replace=TRUE)
> r
[1] 0 1 0 1 1 0 0 1 0 1> v = 1:10
> ifelse(r == 1,v-1,v)
[1] 1 1 3 3 4 6 7 7 9 9
- Phil Spector
Statistical Computing Facility
Department of Statistics
UC Berkeley
spector at stat.berkeley.edu
On Fri, 1 Oct 2010, yunjiangster wrote:
>
> Hi,
> I am looking for a one-liner code for the following situation
> say I have a vector r of 0 and 1's, and another vector v of equal
length. I
> want to assign v[i]-1 to v[i] if r[i]=1, for all i. How would I do that
> using mapply or any other batch processing keywords? thanks.
>
> John
> --
> View this message in context:
http://r.789695.n4.nabble.com/element-wise-conditional-assignment-in-r-tp2951963p2951963.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.
>
You've tried: v - r On Fri, Oct 1, 2010 at 6:53 PM, yunjiangster <yunjiangster@gmail.com> wrote:> > Hi, > I am looking for a one-liner code for the following situation > say I have a vector r of 0 and 1's, and another vector v of equal length. I > want to assign v[i]-1 to v[i] if r[i]=1, for all i. How would I do that > using mapply or any other batch processing keywords? thanks. > > John > -- > View this message in context: > http://r.789695.n4.nabble.com/element-wise-conditional-assignment-in-r-tp2951963p2951963.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]