Say I have a vector in R:
x <- c(1,2,3)
There is a simplified way to create a new vector y, which is smaller than the size x, where:
y <- x[i+1] - x[i]
without using a for loop?
diff(x) is the obvious answer.
diff(x)
A simpler alternative is x[-1] - x[-length(x)] , and this can easily be adapted, for example, to sums or products of consecutive terms
x[-1] - x[-length(x)]
You can use "diff" to get the difference between two consecutive items in a list,
example:
can help you.
Source: https://habr.com/ru/post/909225/More articles:Auto horizontal scrolling in TextView - androidAnother simple random walk simulation using Python (two-dimensional) - pythonHow to insert a new element between all elements of a Ruby array? - arraysDoes onClickListener affect the user interface until all the code inside it runs on Android? - javaWhy in C ++ 11 there is no vector (size_type n, const Allocator & alloc)? - c ++Get function name from inside - javascriptR: how can I calculate the difference between two cells in a data frame and save them in a new column - rThe javascript function knows its name - javascriptConvert joda LocalTime to sql Time - javaHow are database triggers implemented inside the SQL database engine? - sqlAll Articles