Looks like an easy task, can't figure out a simpler way. I have an x
vector below, and need to create group names for consecutive values. My attempt was using rle
, better ideas?
# data
x <- c(1,1,1,2,2,2,3,2,2,1,1)
# make groups
rep(paste0("Group_", 1:length(rle(x)$lengths)), rle(x)$lengths)
# [1] "Group_1" "Group_1" "Group_1" "Group_2" "Group_2" "Group_2" "Group_3" "Group_4"
# [9] "Group_4" "Group_5" "Group_5"
paste0('groupe_', c(1,1,1,2,2,2,3,2,2,1,1))
– Stomy1
is aGroup_1
, and last1
is aGroup_5
– Benitezrle
makes use ofdiff
as @Roland did below. – RevulsionGroup_x
names to the actual values & run lengths? That is, what's the point of this exercise? – Relishnames(x) <- myGroups
. My actual data is data.frame, so I can apply the same and create aGroup
column for aggregate functions down the line. – Benitez