Grab first sequence for each unique values in a column

ledzep

Point Mass at Zero
Dear R Users,

I want to keep only the very first sequence for a given number.

Example Data
Code:
d.f<-data.frame(var1=c(5,5,5,1,1,2,2,5,5,3,1,1,1))
>d.f
var1
1     5
2     5
3     5
4     1
5     1
6     2
7     2
8     5
9     5
10    3
11    1
12    1
13    1

# Unique Values
> unique(d.f)
var1
1     5
4     1
6     2
10    3
For each unique values in the column, here, they are 5,1,2 and 3. I want to keep only the sequence which appeared for the first time.
For example, I want to keep the first 3 rows for 5. The reappearances are to be discarded. e.g: 5 appears in row 8 and 9 again, which will be thrown away. Same for the rest of the values.

Code:
# the desired output will look like this

var1
1     5
2     5
3     5
4     1
5     1
6     2
7     2
10    3
Many Thanks.

Dason

df is the data frame of interest and id.col is the column that contains the sequences.
Code:
fun <- function(df, id.col = 1){

j <- df[,id.col]
spots <- c(1, which(diff(c(j[1], j)) != 0))
difs <- diff(c(spots, length(j)+1))

# Only consider the spots that aren't duplicates
idx <- which(!duplicated(j[spots]))

start <- spots[idx]
end <- start + difs[idx] - 1

return(do.call(rbind, lapply(seq(idx), function(i){df[start[i]:end[i],,drop = FALSE]})))
}

j <- c(5,5,5,1,1,2,2,5,5,3,1,1,1)
df <- data.frame(j = j, k = 1:13)
fun(df, 1)

df <- data.frame(j = 1:10, k = rnorm(10))
fun(df)

df <- data.frame(j = c(1,2,1:10), k = 1:12)
fun(df)