Miki Parr
Miki Parr

Reputation: 111

How to create a sequence column for repeated values?

I have a df that is created like so:

ID <- c(1, 2, 3, 4)
count <- c(2, 4, 6, 10)
data <- data.frame(ID, count)
data <- data[rep(1:nrow(data), data$count),]
print(data)

I want to have another column that essentially prints the sequence of the count? I'm not sure how best to describe what I'm getting at basically I want it to look like the following output:

    ID count sequence
1    1     2        1
1.1  1     2        2
2    2     4        1
2.1  2     4        2
2.2  2     4        3
2.3  2     4        4
3    3     6        1
3.1  3     6        2
3.2  3     6        3
3.3  3     6        4
3.4  3     6        5
3.5  3     6        6
4    4    10        1
4.1  4    10        2
4.2  4    10        3
4.3  4    10        4
4.4  4    10        5
4.5  4    10        6
4.6  4    10        7
4.7  4    10        8
4.8  4    10        9
4.9  4    10       10

Thanks

Upvotes: 3

Views: 457

Answers (3)

user12728748
user12728748

Reputation: 8516

You could also use by:

data$sequence <- 
unlist(with(data, by(ID, ID, seq_along)), use.names=FALSE)

or data.table:

data.table::data.table(data)[, sequence := seq_len(.N), by=ID][]

Upvotes: 1

akrun
akrun

Reputation: 887991

We can use rowid

library(dplyr)
library(data.table)
data %>% 
    mutate(sequence = rowid(ID))
#     ID count sequence
#1    1     2        1
#1.1  1     2        2
#2    2     4        1
#2.1  2     4        2
#2.2  2     4        3
#2.3  2     4        4
#3    3     6        1
#3.1  3     6        2
#3.2  3     6        3
#3.3  3     6        4
#3.4  3     6        5
#3.5  3     6        6
#4    4    10        1
#4.1  4    10        2
#4.2  4    10        3
#4.3  4    10        4
#4.4  4    10        5
#4.5  4    10        6
#4.6  4    10        7
#4.7  4    10        8
#4.8  4    10        9
#4.9  4    10       10

If it is based on the 'count' column

data %>%
     mutate(sequence = rowid(count))

Or using ave from base R

data$sequence <- with(data, ave(seq_along(ID), ID, FUN = seq_along))

Upvotes: 3

Sathish
Sathish

Reputation: 12723

data$sequence <- unlist(lapply( with(data, rle(count)$lengths), seq_len))
data
   ID count sequence
1    1     2        1
1.1  1     2        2
2    2     4        1
2.1  2     4        2
2.2  2     4        3
2.3  2     4        4
3    3     6        1
3.1  3     6        2
3.2  3     6        3
3.3  3     6        4
3.4  3     6        5
3.5  3     6        6
4    4    10        1
4.1  4    10        2
4.2  4    10        3
4.3  4    10        4
4.4  4    10        5
4.5  4    10        6
4.6  4    10        7
4.7  4    10        8
4.8  4    10        9
4.9  4    10       10

Upvotes: 3

Related Questions