vendredi 2 avril 2021

Create subsets based on numberic repetitive pattern in rows

I would like to subset all the rows from a dataframe based on a numeric pattern from one of the columns. In this example, the dataframe looks like:

df <- data.frame(x = c(0:20), y=c(50:70), m = c(0, 0, 0, 0, -1, 0, 0, 1, 0, 0, -1, 0 ,0 , -1, 0, 0, 1, 0, 0, -1, 0))

Pattern: -1, 1, -1.

Therefore, I would like to take the rows that start with -1, contain 1 and end with -1, and all the ones in between. The condition is that always there has to be a 1 between both -1, excluding the possible subsets from -1 to -1 only.

One of the subsets would look like:

Subset1 <- data.frame(x= c(4:10), y = c(54:60), m = c(-1, 0, 0, 1, 0, 0, -1))

I have been trying a lot and I can't figure out how to do it. I have tryied with mapply or for loops but always I'm stuck when it comes to set up the pattern, since both ends of the pattern are the same.

For instance, with mapply, I've done:

List_subsets <- mapply(function(i,j,z) df[i:j:z, , drop = FALSE], -1, 1, -1, SIMPLIFY = FALSE)

But of course, I always get the error: In i:j:z : numerical expression has 3 elements: only the first used.

Do you know if that's possible and could you help me out? I would really appreciate your input, since I'm very new to R and it's very challenging for me.

Thanks a lot in advance!

Aucun commentaire:

Enregistrer un commentaire