如何在达到某个值后删除用户的所有观察值
How to Remove all the observation for a user after he reach certain value
大家好,我发现了一些与我需要做的类似的事情,但它不适用于我的完整数据
How to remove rows after a particular observation is seen for the first time
我需要做的是在Variable状态下client达到4后删除每一个observation,前面的答案link只给出一个client的解决方案
数据
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")
感谢您的帮助
我将展示一个艺术但高效的代码:
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
很遗憾,我无法使用您的数据进行尝试,因为数据格式已损坏!
我们可以使用:
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
>
大家好,我发现了一些与我需要做的类似的事情,但它不适用于我的完整数据
How to remove rows after a particular observation is seen for the first time
我需要做的是在Variable状态下client达到4后删除每一个observation,前面的答案link只给出一个client的解决方案
数据 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")
感谢您的帮助
我将展示一个艺术但高效的代码:
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
很遗憾,我无法使用您的数据进行尝试,因为数据格式已损坏!
我们可以使用:
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
>