i simply want a set of numbers in a vector which: in the vector v, i want the numbers be (x[1]*x[2]*....*x[100], x[2]*x[3]*...*x[100], x[3]*x[4]*...*x[100],...,x[98]*x[99]*x[100], x[99]*x[100],x[100]) so there should be 100 elements in the vector. here's how i do it x=rnorm(100,0.5,0.2) v=prod(x[(1:100):100]) but it turns out failed as it says there are 100 elements in the expression, but only the first one has been used anyone help me how do i get the number i want? thanks soooo much!! -- View this message in context: http://r.789695.n4.nabble.com/help-warning-only-the-first-element-has-been-used-tp3505837p3505837.html Sent from the R help mailing list archive at Nabble.com.
Dennis Murphy
2011-May-07 17:27 UTC
[R] help!! warning:only the first element has been used
Hi: Try sapply(1:length(x), function(y) prod(x[y:length(x)])) HTH, Dennis On Sat, May 7, 2011 at 9:37 AM, pwldk <pwldk at hotmail.com> wrote:> i simply want a set of numbers in a vector which: > in the vector v, i want the numbers be (x[1]*x[2]*....*x[100], > x[2]*x[3]*...*x[100], x[3]*x[4]*...*x[100],...,x[98]*x[99]*x[100], > x[99]*x[100],x[100]) > so there should be 100 elements in the vector. > here's how i do it > x=rnorm(100,0.5,0.2) > v=prod(x[(1:100):100]) > but it turns out failed as it says there are 100 elements in the expression, > but only the first one has been used > > anyone help me how do i get the number i want? > thanks soooo much!! > > > -- > View this message in context: http://r.789695.n4.nabble.com/help-warning-only-the-first-element-has-been-used-tp3505837p3505837.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. >
Ravi Varadhan
2011-May-07 17:29 UTC
[R] help!! warning:only the first element has been used
x <- 1:5 rev(cumprod(rev(x)))> rev(cumprod(rev(x)))[1] 120 120 60 20 5>Ravi. ________________________________________ From: r-help-bounces at r-project.org [r-help-bounces at r-project.org] On Behalf Of pwldk [pwldk at hotmail.com] Sent: Saturday, May 07, 2011 12:37 PM To: r-help at r-project.org Subject: [R] help!! warning:only the first element has been used i simply want a set of numbers in a vector which: in the vector v, i want the numbers be (x[1]*x[2]*....*x[100], x[2]*x[3]*...*x[100], x[3]*x[4]*...*x[100],...,x[98]*x[99]*x[100], x[99]*x[100],x[100]) so there should be 100 elements in the vector. here's how i do it x=rnorm(100,0.5,0.2) v=prod(x[(1:100):100]) but it turns out failed as it says there are 100 elements in the expression, but only the first one has been used anyone help me how do i get the number i want? thanks soooo much!! -- View this message in context: http://r.789695.n4.nabble.com/help-warning-only-the-first-element-has-been-used-tp3505837p3505837.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.
thanks a lot to u guys ! both works well! -- View this message in context: http://r.789695.n4.nabble.com/help-warning-only-the-first-element-has-been-used-tp3505837p3506459.html Sent from the R help mailing list archive at Nabble.com.