R: Keep the first and last two rows of the dataframe and meanwhile deleting one rows of other middle dataframe

Viewed 64

I have a very specific question about data cleaning in R and am not sure how should I achieve this. Basically, you can find below an example data frame.

test <- data.frame(personID = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 
                   sequenceTrip = c(1, 1, 2, 2, 3, 3, 4, 4), departureHour = c(9L, 
                                                                               9L, 13L, 13L, 14L, 14L, 16L, 16L), departureMinute = c(34L, 
                                                                                                                                      34L, 34L, 34L, 35L, 35L, 19L, 19L), tripRangeTypeOrigin = c(0, 
                                                                                                                                                                                                  0, 1, 1, 0, 0, 1, 1), arrivalHour = c(10, 10, 14, 14, 15, 
                                                                                                                                                                                                                                        15, 18, 18), arrivalMinute = c(34, 34, 34, 34, 49, 49, 4, 
                                                                                                                                                                                                                                                                       4), tripRangeTypeDestin = c(1, 1, 0, 0, 1, 1, 0, 0), tripPurpose = c("leisure", 
                                                                                                                                                                                                                                                                                                                                            "leisure", "return home", "return home", "shopping", "shopping", 
                                                                                                                                                                                                                                                                                                                                            "return home", "return home"), mode = c("car", "car", "car", 
                                                                                                                                                                                                                                                                                                                                                                                    "car", "car", "car", "car", "car"), tripDistance = c(77, 
                                                                                                                                                                                                                                                                                                                                                                                                                                         77, 77, 77, 100, 100, 115, 115), typicalDurationNextActivity = c(10800, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          10800, 60, 60, 1800, 1800, 24960, 24960), arrivalTimeInMInute = c(NA, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            NA, NA, NA, NA, NA, 1084, 1084), originDestinType = c(0, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  1, 0, 1, 0, 1, 0, 1), tripNB = c("MUTSAARD", "Outside Brussels", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   "Outside Brussels", "HEYSEL", "HEYSEL", "Outside Brussels", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   "Outside Brussels", "CHAUSSEE DE WAVRE - SAINT-JULIEN "))

What I want to achieve is keeping the 1st, 2nd, second last (row 7 in this case) and last (row 8 in this case) rows of this dataframe, meanwhile, for rows 3:6, I want to filter out the rows which originDestinType == 1. So the final data frame would be 6 rows in total. I know this can be achieved by command like rbind, however, I am wondering if there is an easier way for achieving this using package like dplyr?

Thanks very much for your help!

2 Answers

This should be good :

df <- test %>% filter(rownames(test) %in% c(1,2,7,8) | originDestinType!=1)

Update after clarification: Try this:

library(dplyr)
test %>% 
  groub_by(yourgroup) %>%
  filter(row_number() == first(row_number(),2) |
           row_number() == last(row_number(),2) |
             originDestinType == 1)
  1. List item

Here is a dplyr solution: We could use filter with | and & operator and first and last function:

library(dplyr)
test %>% 
  filter(row_number() == first(row_number(),2) |
           row_number() == last(row_number(),2) |
           row_number() %in% (3:6) & originDestinType == 1)

output:

  originDestinType                            tripNB
1                0                          MUTSAARD
2                1                  Outside Brussels
3                1                            HEYSEL
4                1                  Outside Brussels
5                0                  Outside Brussels
6                1 CHAUSSEE DE WAVRE - SAINT-JULIEN 
Related