r语言 - 如何仅在缺少其他值时才在第一次实例之后删除后续值



当所有未来值均为 0 时,我正在尝试在零的第一个实例之后删除零。最终,我很想group_by物种做这件事,但婴儿步骤。下面是一个示例;

# Sample
library(tidyverse)
id<-c("a","b","c","d","e","f","g","h","i","j")
time<-c(1,2,3,4,5,6,7,8,9,10)
value<-c(90, 50, 40, 0, 30, 30, 0, 10, 0, 0)
df<-data.frame(id, time, value)
df
id time value
1   a    1    90
2   b    2    50
3   c    3    40
4   d    4     0
5   e    5    30
6   f    6    30
7   g    7     0
8   h    8    10
9   i    9     0
10  j   10     0

我希望看到观察 ID "j",并且只删除观察 ID "j"。我什至不知道从哪里开始。任何建议都非常感谢!

仅在基本 R 中。它使用rle来获取尾随零的数量(如果有(。然后用head对数据帧进行子集。

r <- rle(df$value == 0)
if(r$values[length(r$values)]) head(df, -(r$lengths[length(r$values)] - 1))
#  id time value
#1  a    1    90
#2  b    2    50
#3  c    3    40
#4  d    4     0
#5  e    5    30
#6  f    6    30
#7  g    7     0
#8  h    8    10
#9  i    9     0

您可以使用上面的代码编写一个函数,也可以将其*apply到组中。

trailingZeros <- function(DF, col = "value"){
r <- rle(DF[[col]] == 0)
if(r$values[length(r$values)] && r$lengths[length(r$values)] > 1)
head(DF, -(r$lengths[length(r$values)] - 1))
else
DF
}
trailingZeros(df)

请注意,这也适用于大量尾随零。

id2 <- c("a","b","c","d","e","f","g","h","i","j","k")
time2 <- c(1,2,3,4,5,6,7,8,9,10,11)
value2 <- c(90, 50, 40, 0, 30, 30, 0, 10, 0, 0, 0)    # One more zero at end
df2 <- data.frame(id = id2, time = time2, value = value2)
trailingZeros(df2)

这里是 tidyverse 中的一个解决方案,它也适用于大量尾随零:

df <- tibble(id = letters[1:11], time = 1:11, 
value = c(90,50,40,0,30,30,0,10,0,0,0))
df %>% 
slice(n():1) %>% 
slice(c(which(cumsum(value > 0) > 0)[1] - 1, which(cumsum(value > 0) > 0))) %>% 
slice(n():1)

适用于组的整洁解决方案

基于样本数据(无分组( 代码可以缩短,但这看起来非常可读;-(

df %>% 
#arrange by id
arrange( id ) %>%
#no grouping valiable in sample data.. so don't use group_by here
#group_by( group) %>%
#create dummy's: position in group, last value of group, position of last non-zero in group, previous value (within group)
mutate( pos_in_group = 1:n() ) %>%
mutate( last_value = last( value ) ) %>%
mutate( pos_last_not_zero = max( which( value != 0) ) ) %>%
mutate( prev_value = lag( value ) ) %>%
#filter all rows where: 
#   the last value of the group != 0 AND 
#   the previous row (within the group) != 0 AND 
#  the position of the row is 'below' the last non-zero measurement (in the group)
filter( !(last_value == 0 & prev_value == 0 & pos_in_group >= pos_last_not_zero + 1 ) ) %>%
#throw away the dummy's
select( -c( pos_in_group, last_value, pos_last_not_zero, prev_value ) )
#   id time value
# 1  a    1    90
# 2  b    2    50
# 3  c    3    40
# 4  d    4     0
# 5  e    5    30
# 6  f    6    30
# 7  g    7     0
# 8  h    8    10
# 9  i    9     0

涉及一些分组的示例

# Sample
library(tidyverse)
id<-c("a","b","c","d","e","f","g","h","i","j","k")
group<-c(1,1,1,1,1,1,2,2,2,2,2)
time<-c(1,2,3,4,5,6,7,8,9,10,11)
value = c(90,0,0,40,0,0,30,30,0,0,0)
df<-data.frame(id, group, time, value)
df
#    id group time value
# 1   a     1    1    90
# 2   b     1    2     0
# 3   c     1    3     0
# 4   d     1    4    40
# 5   e     1    5     0
# 6   f     1    6     0
# 7   g     2    7    30
# 8   h     2    8    30
# 9   i     2    9     0
# 10  j     2   10     0
# 11  k     2   11     0
df %>% 
#arrange by id
arrange( id ) %>%
#group
group_by( group) %>%
#create dummy's: position in group, last value of group, position of last non-zero in group, previous value (within group)
mutate( pos_in_group = 1:n() ) %>%
mutate( last_value = last( value ) ) %>%
mutate( pos_last_not_zero = max( which( value != 0) ) ) %>%
mutate( prev_value = lag( value ) ) %>%
#filter all rows where: 
#   the last value of the group != 0 AND 
#   the previous row (within the group) != 0 AND 
#  the position of the row is 'below' the last non-zero measurement (in the group)
filter( !(last_value == 0 & prev_value == 0 & pos_in_group >= pos_last_not_zero + 1 ) ) %>%
#throuw away the dummy's
select( -c( pos_in_group, last_value, pos_last_not_zero, prev_value ) )
# # A tibble: 8 x 4
# # Groups:   group [2]
#   id    group  time value
#   <fct> <dbl> <dbl> <dbl>
# 1 a         1     1    90
# 2 b         1     2     0
# 3 c         1     3     0
# 4 d         1     4    40
# 5 e         1     5     0
# 6 g         2     7    30
# 7 h         2     8    30
# 8 i         2     9     0

相关内容

最新更新