Reputation: 73
I have two columns x
and y
, I am trying to create a new sequenced column z
if the value in one of the columns is non-unique. e.g.
x <- c("1", "1", "1", "1", "2", "2", "2", "3", "3", "3", "4", "4", "5", "6", "6", "6")
y <- c("Y", "Y", "Y", "Y", "N", "N", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "N", "Y", "Y")
df <- data.frame(x, y)
What I would like to get is the following:
# x y z
#
# 1 Y 1
# 1 Y 2
# 1 Y 3
# 1 Y 4
# 2 N 1
# 2 N 2
# 2 Y 3
# 3 Y 1
# 3 Y 2
# 3 Y 3
# 4 Y 1
# 4 Y 2
# 5 Y 1
# 6 N 1
# 6 Y 2
# 6 Y 3
Upvotes: 0
Views: 117
Reputation: 73
y <- c("Y", "Y", "Y", "Y", "N", "N", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "N", "Y", "Y")
df <- data.frame(x, y)
df$z <- ave(df$x, df$x, FUN = seq_along)
gives
x y z
1 1 Y 1
2 1 Y 2
3 1 Y 3
4 1 Y 4
5 2 N 1
6 2 N 2
7 2 Y 3
8 3 Y 1
9 3 Y 2
10 3 Y 3
11 4 Y 1
12 4 Y 2
13 5 Y 1
14 6 N 1
15 6 Y 2
16 6 Y 3
Upvotes: 1