Reputation: 77
I need in 2nd column a count of how many times observation is in 1st column. Here, 2nd column should have values 1,2,3,1,2,1,2. This code doesn't work.
x <- c(11,11,11,22,22,33,33) y <- c(1,1,1,1,1,1,1) df <- data.frame(x,y)
i <- 1 for (i in 1:(nrow(df)-1)){
if(df[i+1,1] == df[i,1]){df[i+1,2] <- 2}
if(df[i+2,1] == df[i,1]){df[i+2,2 <- 3}
else df[i+2,2] <- 1}
Upvotes: 1
Views: 49
Reputation: 38520
You could also use sequence
and rle
like
sequence(rle(x)$length)
[1] 1 2 3 1 2 1 2
So
df$y <- sequence(rle(x)$length)
rle
produces an object with the lengths of runs of repeated values as well as the values themselves. By feeding these lengths to sequence
, we can produce counts from 1 to each of these values.
Upvotes: 0
Reputation: 17668
You can try base R:
unlist(by(x, x, seq_along), use.names = F)
# or
ave(x, x, FUN = seq_along)
Upvotes: 1
Reputation: 1910
Here's a solution:
require(dplyr)
df %>% group_by(x) %>% mutate(z=cumsum(y)) %>% ungroup()
# A tibble: 7 × 3
# x y z
# <dbl> <dbl> <dbl>
# 1 11 1 1
# 2 11 1 2
# 3 11 1 3
# 4 22 1 1
# 5 22 1 2
# 6 33 1 1
# 7 33 1 2
Upvotes: 3