0

Hi everyone I found something similar to what I need to do but it doesn't work with my full data

How to remove rows after a particular observation is seen for the first time

What I need to do is to delete every observation after the client reach 4 in the Variable state, the answer in the previous link give the solution for only one client.

The data Problem

If anyone needs the data:

structure(list(mes = structure(c(2016, 2016.08333333333,2016.16666666667,2016.25, 2016.33333333333, 2016.41666666667,2016.5, 2016.58333333333,2016.66666666667, 2016.75, 2016.83333333333, 2016.91666666667,2016, 2016.08333333333, 2016.16666666667, 2016.25, 2016.33333333333, 2016.41666666667, 2016.5, 2016.58333333333, 2016.66666666667,2016.75, 2016.83333333333, 2016.91666666667), class = "yearmon"),inicio = structure(c(2002.25, 2002.25, 2002.25, 2002.25, 
2002.25, 2002.25, 2002.25, 2002.25, 2002.25, 2002.25, 2002.25, 
2002.25, 2004.41666666667, 2004.41666666667, 2004.41666666667, 
2004.41666666667, 2004.41666666667, 2004.41666666667, 2004.41666666667, 
2004.41666666667, 2004.41666666667, 2004.41666666667, 2004.41666666667, 
2004.41666666667), class = "yearmon"), diff = c(13.75, 13.8333333333333, 
13.9166666666667, 14, 14.0833333333333, 14.1666666666667, 
14.25, 14.3333333333333, 14.4166666666667, 14.5, 14.5833333333333, 
14.6666666666667, 11.5833333333333, 11.6666666666665, 11.75, 
11.8333333333333, 11.9166666666665, 12, 12.0833333333333, 
12.1666666666665, 12.25, 12.3333333333333, 12.4166666666665, 
12.5), code = c("0000040542281157798", "0000040542281157798", 
"0000040542281157798", "0000040542281157798", "0000040542281157798", 
"0000040542281157798", "0000040542281157798", "0000040542281157798", 
"0000040542281157798", "0000040542281157798", "0000040542281157798", 
"0000040542281157798", "00000803019311630270", "00000803019311630270", 
"00000803019311630270", "00000803019311630270", "00000803019311630270", 
"00000803019311630270", "00000803019311630270", "00000803019311630270", 
"00000803019311630270", "00000803019311630270", "00000803019311630270", 
"00000803019311630270"), state = c(1, 1, 1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 1, 1, 1, 1, 4, 4, 4, 4, 4, 4, 4, 4)), .Names = c("mes","inicio", "diff","code", "state"), row.names = c(NA,24L), class = "data.frame")

Thanks for your help

2 Answers2

0

I am going to present an artistic, but efficient code:

df[diff(df$code) == 0 &  # to select same users
   diff(df$State == 4) != 1 &  # to exclude first occurence of 4
   df$State == 4,  # to select all other fours
   ] <- NULL

Unfortunately I cannot try it with your data, as the data format is broken!

tobiaspk1
  • 378
  • 1
  • 11
0

We can use:

library(tidyverse)
data%>%group_by(CODCOSECHA)%>%slice(1:anyDuplicated(cumsum(state==1)))
# A tibble: 9 x 6
# Groups:   CODCOSECHA [2]
       mes   inicio     diff                 code state CODCOSECHA
     <dbl>    <dbl>    <dbl>                <chr> <dbl>      <int>
1 2016.000 2002.250 13.75000  0000040542281157798     1          1
2 2016.083 2002.250 13.83333  0000040542281157798     1          1
3 2016.167 2002.250 13.91667  0000040542281157798     1          1
4 2016.250 2002.250 14.00000  0000040542281157798     4          1
5 2016.000 2004.417 11.58333 00000803019311630270     1          2
6 2016.083 2004.417 11.66667 00000803019311630270     1          2
7 2016.167 2004.417 11.75000 00000803019311630270     1          2
8 2016.250 2004.417 11.83333 00000803019311630270     1          2
9 2016.333 2004.417 11.91667 00000803019311630270     4          2
> 
Onyambu
  • 67,392
  • 3
  • 24
  • 53