Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于R中两个嵌套id变量的二进制变量的编码状态随时间的变化_R_Dataframe_Dplyr - Fatal编程技术网

基于R中两个嵌套id变量的二进制变量的编码状态随时间的变化

基于R中两个嵌套id变量的二进制变量的编码状态随时间的变化,r,dataframe,dplyr,R,Dataframe,Dplyr,我有嵌套在产品(prod_id)中的卖家(seller_id)和随时间变化的二分法变量(dich)。在任何给定时间点,每个产品只能有一个DIC=1的卖家。我想做的是让拥有1的卖家在一段时间内拥有1的值,直到发现另一个卖家的dich=1。数据按时间排序(降序),如果需要我澄清,请告诉我。谢谢 test <- data.frame('prod_id'= c("shoe", "shoe", "shoe", "shoe", "shoe", "shoe", "boat", "boat","boat"

我有嵌套在产品(prod_id)中的卖家(seller_id)和随时间变化的二分法变量(dich)。在任何给定时间点,每个产品只能有一个DIC=1的卖家。我想做的是让拥有1的卖家在一段时间内拥有1的值,直到发现另一个卖家的dich=1。数据按时间排序(降序),如果需要我澄清,请告诉我。谢谢

test <- data.frame('prod_id'= c("shoe", "shoe", "shoe", "shoe", "shoe", "shoe", "boat", "boat","boat","boat","boat","boat"), 
               'seller_id'= c("a", "a", "b", "c", "c", "a", "a","a", "b", "b", "c", "b"), 
               'Dich'= c(1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0), 
               'time'= c("10/25/2017 9:40", "11/8/2017 9:36", "11/14/2017 21:02", "11/29/2017 23:20", "12/5/2017 20:30",
                "12/10/2017 17:38", "12/26/2017 8:00", "1/27/2018 6:26", "4/10/2018 5:40",
                "4/24/2018 20:16", "5/18/2018 21:52", "8/9/2018 9:52") )
理想结果:

     prod_id  seller_id Dich          time
1     shoe         a    1  10/25/2017 9:40
2     shoe         a    1   11/8/2017 9:36
3     shoe         b    0 11/14/2017 21:02
4     shoe         c    1 11/29/2017 23:20
5     shoe         c    1  12/5/2017 20:30
6     shoe         a    0 12/10/2017 17:38
7     boat         a    0  12/26/2017 8:00
8     boat         a    0   1/27/2018 6:26
9     boat         b    1   4/10/2018 5:40
10    boat         b    1  4/24/2018 20:16
11    boat         c    0  5/18/2018 21:52
12    boat         b    1    8/9/2018 9:52

一种方法是创建一个
last_seller
列来跟踪最后一个卖家的身份。例如

library(dplyr)
library(tidyr)

test %>%
  group_by(prod_id) %>%
  mutate(seller_id = as.character(seller_id), 
         last_seller = ifelse(Dich == 1, seller_id, NA)) %>%
  fill(last_seller) %>%
  mutate(Dich1 = ifelse((seller_id != last_seller) | is.na(last_seller), 0, 1))
#    prod_id seller_id  Dich time             last_seller Dich1
#    <fct>   <chr>     <dbl> <fct>            <chr>       <dbl>
#  1 boat    a             0 12/26/2017 8:00  NA              0
#  2 boat    a             0 1/27/2018 6:26   NA              0
#  3 boat    b             1 4/10/2018 5:40   b               1
#  4 boat    b             0 4/24/2018 20:16  b               1
#  5 boat    c             0 5/18/2018 21:52  b               0
#  6 boat    b             0 8/9/2018 9:52    b               1
#  7 shoe    a             1 10/25/2017 9:40  a               1
#  8 shoe    a             0 11/8/2017 9:36   a               1
#  9 shoe    b             0 11/14/2017 21:02 a               0
# 10 shoe    c             1 11/29/2017 23:20 c               1
# 11 shoe    c             0 12/5/2017 20:30  c               1
# 12 shoe    a             0 12/10/2017 17:38 c               0
库(dplyr)
图书馆(tidyr)
测试%>%
分组依据(产品id)%>%
变异(卖方id=as.character(卖方id),
最后一个卖方=ifelse(Dich==1,卖方id,NA))%>%
填充(最后一个卖家)%>%
变异(Dich1=ifelse((卖方id!=最后卖方)| is.na(最后卖方),0,1))
#产品id卖家id上次卖家DIC1时间
#                                
#1船a 0 2017年12月26日8:00 NA 0
#2船a 0 2018年1月27日6:26 NA 0
#3船b 1 2018年10月4日5:40 b 1
#4船b 0 2018年4月24日20:16 b 1
#5船c 0 2018年5月18日21:52 b 0
#6船b 0 2018年8月9日9:52 b 1
#7鞋款a 1 2017年10月25日9:40 a 1
#8鞋a 0 2017年11月8日9:36 a 1
#9鞋b 0 2017年11月14日21:02 a 0
#10鞋c 1 2017年11月29日23:20 c 1
#11鞋c 0 2017年12月5日20:30 c 1
#12鞋款a 0 2017年10月12日17:38 c 0

一种方法是创建一个
last\u seller
列来跟踪最后一个卖家的身份。例如

library(dplyr)
library(tidyr)

test %>%
  group_by(prod_id) %>%
  mutate(seller_id = as.character(seller_id), 
         last_seller = ifelse(Dich == 1, seller_id, NA)) %>%
  fill(last_seller) %>%
  mutate(Dich1 = ifelse((seller_id != last_seller) | is.na(last_seller), 0, 1))
#    prod_id seller_id  Dich time             last_seller Dich1
#    <fct>   <chr>     <dbl> <fct>            <chr>       <dbl>
#  1 boat    a             0 12/26/2017 8:00  NA              0
#  2 boat    a             0 1/27/2018 6:26   NA              0
#  3 boat    b             1 4/10/2018 5:40   b               1
#  4 boat    b             0 4/24/2018 20:16  b               1
#  5 boat    c             0 5/18/2018 21:52  b               0
#  6 boat    b             0 8/9/2018 9:52    b               1
#  7 shoe    a             1 10/25/2017 9:40  a               1
#  8 shoe    a             0 11/8/2017 9:36   a               1
#  9 shoe    b             0 11/14/2017 21:02 a               0
# 10 shoe    c             1 11/29/2017 23:20 c               1
# 11 shoe    c             0 12/5/2017 20:30  c               1
# 12 shoe    a             0 12/10/2017 17:38 c               0
库(dplyr)
图书馆(tidyr)
测试%>%
分组依据(产品id)%>%
变异(卖方id=as.character(卖方id),
最后一个卖方=ifelse(Dich==1,卖方id,NA))%>%
填充(最后一个卖家)%>%
变异(Dich1=ifelse((卖方id!=最后卖方)| is.na(最后卖方),0,1))
#产品id卖家id上次卖家DIC1时间
#                                
#1船a 0 2017年12月26日8:00 NA 0
#2船a 0 2018年1月27日6:26 NA 0
#3船b 1 2018年10月4日5:40 b 1
#4船b 0 2018年4月24日20:16 b 1
#5船c 0 2018年5月18日21:52 b 0
#6船b 0 2018年8月9日9:52 b 1
#7鞋款a 1 2017年10月25日9:40 a 1
#8鞋a 0 2017年11月8日9:36 a 1
#9鞋b 0 2017年11月14日21:02 a 0
#10鞋c 1 2017年11月29日23:20 c 1
#11鞋c 0 2017年12月5日20:30 c 1
#12鞋款a 0 2017年10月12日17:38 c 0

非常感谢王卫煌!非常感谢王卫煌!