Spread () based on id and value in another column

I was already looking for a few examples, but I did not find any filtered allowed values ​​and their dates.

Date <-c('3/13/2017 6:21', '3/20/2017 6:28','3/13/2017 6:22','3/20/2017 6:28',' 3/13/2017 6:23','3/20/2017 6:28','3/13/2017 6:24',' 3/20/2017 6:28', ' 3/24/2017 6:28')
Enabled_value<-c(0,1,0,1,0,1,0,1,0)
Helper<-c('39RTU1','39RTU1','39RTU2','39RTU2','39RTU2','39RTU3','39RTU3','39RTU4','39RTU4', '39RTU4')

:

Helper      Date(Enabled Value =0)     Date (Enabled Value =1) 
39RTU1       3/13/2017 6:20            3/20/2017 6:28
39RTU2       3/13/2017 6:21            3/20/2017 6:28
39RTU3       3/13/2017 6:22            3/20/2017 6:28
39RTU4       3/13/2017 6:24            3/20/2017 6:28
39RTU4       3/24/2017 6:28

As you can see, I have timestamps for each observation - each line must be an instance (i.e., go from Enabled_value from 0 to 1, and if the last Enabled_value for unit = 0, there should be a new line (see 39RTU4 below )

I have already done a great job reducing this data set (from 500 thousand to 2 thousand rows).

I try to use tidyrand dplyr, but mine spreadcontinues to work with errors.

> sorted_data1<-spread(sorted_data,Enabled_Value,Helper)
Error: Duplicate identifiers for rows (1340, 1342)
+4
source share
1 answer

, . . (. , 10, 9).

, . , , , .

Date <-c('3/13/2017 6:21', '3/20/2017 6:28','3/13/2017 6:22','3/20/2017 6:28',' 3/13/2017 6:23','3/20/2017 6:28','3/13/2017 6:24',' 3/20/2017 6:28', ' 3/24/2017 6:28')
Enabled_value<-c(0,1,0,1,0,1,0,1,0)
Helper<-c('39RTU1','39RTU1','39RTU2','39RTU2','39RTU2','39RTU3','39RTU3','39RTU4','39RTU4')

df <- tibble(Date, Enabled_value, Helper)
df %>% 
  group_by(Helper, Enabled_value) %>% 
  mutate(count = 1:n()) %>% 
  spread(Enabled_value, Date) %>% 
  rename(Enabled_value_0 = `0`,
         Enabled_value_1 = `1`)

# A tibble: 5 x 4
# Groups:   Helper [4]
  Helper count Enabled_value_0 Enabled_value_1
*  <chr> <int>           <chr>           <chr>
1 39RTU1     1  3/13/2017 6:21  3/20/2017 6:28
2 39RTU2     1  3/13/2017 6:22  3/20/2017 6:28
3 39RTU2     2  3/13/2017 6:23            <NA>
4 39RTU3     1  3/13/2017 6:24  3/20/2017 6:28
5 39RTU4     1  3/24/2017 6:28  3/20/2017 6:28
+2

Source: https://habr.com/ru/post/1688553/


All Articles