Lenard Dome
Lenard Dome

Reputation: 112

split vector after all predefined set of elements occured

I have to do the following:

I have a vector, let as say

x  <- c(1, 1, 2, 3, 3, 3, 4, 4, 5, 5, 3, 2, 11, 1, 3, 3, 4, 1)

I have to subset the remainder of a vector after 1, 2, 3, 4 occurred at least once. So the subset new vector would only include 4, 5, 5, 3, 2, 11, 1, 3, 3, 4, 1.

I need a relatively easy solution on how to do this. It might be possible to do an if and while loop with breaks, but I am kinda struggling to come up with a solution.

Is there a simple (even mathematical way) to do this in R?

Upvotes: 2

Views: 57

Answers (2)

Darren Tsai
Darren Tsai

Reputation: 35714

Use sapply to find where each predefined number occurs first time.

x[-seq(max(sapply(1:4, function(y) which(x == y)[1])))]

# [1]  4  5  5  3  2 11  1  3  3  4  1

Data

x <- c(1, 1, 2, 3, 3, 3, 4, 4, 5, 5, 3, 2, 11, 1, 3, 3, 4, 1)

Upvotes: 2

Nikhil Gupta
Nikhil Gupta

Reputation: 1486

You can use run length encoding for this

x  = c(1, 1, 2, 3, 3, 3, 4, 4, 5, 5, 3, 2, 11, 1, 3, 3, 4, 1)
encoded = rle(x)

# Pick the first location of 1, 2, 3, and 4
# Then find the max index location
indices = c(which(encoded$values == 1)[1], 
            which(encoded$values == 2)[1],
            which(encoded$values == 3)[1],
            which(encoded$values == 4)[1])

index = max(indices)

# Find the index of x corresponding to your split location
reqd_index = cumsum(encoded$lengths)[index-1] + 2 

# Print final split value
x[reqd_index:length(x)]

The result is as follows

> x[reqd_index:length(x)]
 [1]  4  5  5  3  2 11  1  3  3  4  1

Upvotes: 2

Related Questions