I have a vector of sorted positive integer values (e.g., postive integers
after applying sort() and unique()). For example, this:
c(1,2,5,6,7,8,25,30,31,32,33)
I want to make a matrix from that vector that has two columns: (1) the
first value in every run of consecutive integer values, and
Miller
Sent: Monday, 5 January 2015 1:03 p.m.
To: R-Help List
Subject: [R] counting sets of consecutive integers in a vector
I have a vector of sorted positive integer values (e.g., postive integers after
applying sort() and unique()). For example, this:
c(1,2,5,6,7,8,25,30,31,32,33)
I want to make
List
Subject: [R] counting sets of consecutive integers in a vector
I have a vector of sorted positive integer values (e.g., postive integers
after applying sort() and unique()). For example, this:
c(1,2,5,6,7,8,25,30,31,32,33)
I want to make a matrix from that vector that has two columns
Here is a solution using data.table
require(data.table)
x - data.table(v, diff = cumsum(c(1, diff(v)) != 1))
x
v diff
1: 10
2: 20
3: 51
4: 61
5: 71
6: 81
7: 252
8: 303
9: 313
10: 323
11: 333
x[, list(value = v[1L], length = .N),
-boun...@r-project.org] On Behalf Of Mike Miller
Sent: Monday, 5 January 2015 1:03 p.m.
To: R-Help List
Subject: [R] counting sets of consecutive integers in a vector
I have a vector of sorted positive integer values (e.g., postive integers after
applying sort() and unique()). For example, this:
c
5 matches
Mail list logo