如何在达到特定值后删除用户的所有观察

问题描述 投票:0回答:2

大家好,我发现了类似于我需要做的事情,但它不适用于我的完整数据

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

我需要做的是在客户端在Variable状态下达到4之后删除每个观察,上一个链接中的答案仅为一个客户端提供解决方案。

数据Problem

如果有人需要这些数据:

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")

谢谢你的帮助

r markov-chains
2个回答
0
投票

我们可以用:

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
> 

0
投票

我将展示一个艺术但有效的代码:

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

不幸的是,由于数据格式被破坏,我无法对您的数据进行尝试!

© www.soinside.com 2019 - 2024. All rights reserved.