Splitting data into a separate Dataframe

I have a dataframe:

entry_df <- data.frame(date = seq(as.Date("2018/01/01"), as.Date("2018/01/07"), 
                       "days"),diff = c(1,0,-1 , 0,0 ,1,-1))

entry_df

        date diff
1 2018-01-01    1
2 2018-01-02    0
3 2018-01-03   -1
4 2018-01-04    0
5 2018-01-05    0
6 2018-01-06    1
7 2018-01-07   -1

I want the rbindlines that are in betwen 1and -1in dataframe, and click on list.

Steps:

  • 2018-01-01is equal 1, therefore it will be my first element of my file frame 2018-01-02is equal 0, therefore I need to bind it to the first data frame is 2018-01-03equal -1, therefore I need to bind it to the first data frame and will contain the first element of the list

  • 2018-01-04and 2018-01-05equal 0, so I do nothing ...

  • 2018-01-06equals 1, so I create a dataframe in it, will be its first element. 2018-01-07is equal -1, therefore I will be attached to the data frame and will be the second element of the list

:

output_list[[1]] <- data.frame(date = seq(as.Date("2018/01/01"), 
                               as.Date("2018/01/03"), "days")) 
output_list[[2]] <- data.frame(date = seq(as.Date("2018/01/06"),
                               as.Date("2018/01/07"), "days")) 

output_list      

[[1]]
        date
1 2018-01-01
2 2018-01-02
3 2018-01-03

[[2]]
        date
1 2018-01-06
2 2018-01-07
+6
1

, 1 -1s , == which, Map 1- 'entry_df'

Map(function(i, j) entry_df[i:j, 1, drop = FALSE], 
           which(entry_df$diff == 1), which(entry_df$diff == -1))
#[[1]]
#        date
#1 2018-01-01
#2 2018-01-02
#3 2018-01-03

#[[2]]
#        date
#6 2018-01-06
#7 2018-01-07
+4

All Articles