Say I have a vector in R:
x <- c(1,2,3)
is there a concise way to create a new vector y that is one less than the size of x where:
y <- x[i+1] - x[i]
without using a for-loop?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
diff(x)is the obvious answer.A more basic alternative is
x[-1] - x[-length(x)]and this can easily be adapted for example to sums or products of consecutive terms