How do I remove offsetting rows in a tibble?

问题内容:

I am trying to remove rows that have offsetting values.

library(dplyr)
a <- c(1, 1, 1, 1, 2, 2, 2, 2,2,2)
b <- c("a", "b", "b", "b", "c", "c","c", "d", "d", "d")
d <- c(10, 10, -10, 10, 20, -20, 20, 30, -30, 30)
o <- c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J")
df <- tibble(ID = a, SEQ = b, VALUE = d, OTHER = o)

Generates this ordered table that is grouped by ID and SEQ.

> df
# A tibble: 10 x 4
      ID   SEQ VALUE OTHER
   <dbl> <chr> <dbl> <chr>
 1     1     a    10     A
 2     1     b    10     B
 3     1     b   -10     C
 4     1     b    10     D
 5     2     c    20     E
 6     2     c   -20     F
 7     2     c    20     G
 8     2     d    30     H
 9     2     d   -30     I
10     2     d    30     J

I want to drop the row pairs (2,3), (5,6), (8,9) because VALUE negates the VALUE in the matching previous row.

I want the resulting table to be

> df2
# A tibble: 4 x 4
     ID   SEQ VALUE OTHER
  <dbl> <chr> <dbl> <chr>
1     1     a    10     A
2     1     b    10     D
3     2     c    20     G
4     2     d    30     J

I know that I can’t use group_by %>% summarize, because I need to keep the value that is in OTHER. I’ve looked at the dplyr::lag() function but I don’t see how that can help. I believe that I could loop through the table with some type of for each loop and generate a logical vector that can be used to drop the rows, but I was hoping for a more elegant solution.

问题评论:

答案:

答案1:

What about:

vec <- c((df$VALUE[-1] + head(df$VALUE,-1))!=0 , TRUE)
df[vec,]
# A tibble: 4 x 4
     ID   SEQ VALUE OTHER
  <dbl> <chr> <dbl> <chr>
1     1     a    10     A
2     1     b    10     D
3     2     c    20     G
4     2     d    30     J

The idea is to take your VALUE field and subtract it with a slightly subset version of it. When the result is 0, than you remove the line.

答案评论:

    
Nice, your solution even worked on an edge case I just discovered. I am going to apply it to my real world problem and see what happens.
– Joe Slone
21 mins ago
    
If you find problem with it, try to identify the problems and make them reproducible in your example.

原文地址:

https://stackoverflow.com/questions/47756408/how-do-i-remove-offsetting-rows-in-a-tibble

Tags:

添加评论

友情链接:蝴蝶教程