Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.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 基于第一个和最后一个值的时间差_R_Dataframe - Fatal编程技术网

R 基于第一个和最后一个值的时间差

R 基于第一个和最后一个值的时间差,r,dataframe,R,Dataframe,我有一个家庭的col1指数,每个家庭的col2指数,每个人的旅行的col3指数,每个人的旅游的col4指数,col5和col6活动的开始和结束时间我如何为每个家庭中的每个人的每次旅行创建一个新列,其开始时间是第一次旅行的开始时间,结束时间是最后一次旅行的结束时间 这里有一个例子 family persons trip tour start time end time 1 1 1 1 3 8:4

我有一个家庭的col1指数,每个家庭的col2指数,每个人的旅行的col3指数,每个人的旅游的col4指数,col5和col6活动的开始和结束时间我如何为每个家庭中的每个人的每次旅行创建一个新列,其开始时间是第一次旅行的开始时间,结束时间是最后一次旅行的结束时间

这里有一个例子

 family    persons    trip    tour   start time   end time
   1      1        1        1         3         8:45
   1      1        2        1       8:45        13:30
   1      1        3        1       13:30       15
   1      1        4        1      15:00        15:30
   1      1        5        2      20:00        22:00
   1      1        6        2      22:00       8:30
   1      2        1        1       3:00        8:00
   1      2        2        1      8:00        17:00
   1      2        3        1      17:00       24:00
   1      3        1        1       8:00       23:00
   1      3        2        1        23:00      24:00
第一人称有2次旅行和6次旅行。在第一次旅行中,第一次旅行在3:00开始,最后一次旅行在15:30结束,在第二次旅行中,开始时间是20:00,最后一次旅行在8:30结束

第二个人有1次旅行和3次旅行。在本次旅行中,第一次旅行的开始时间为3点,最后一次旅行的结束时间为24:00

第三人有1次旅行和2次旅行,第一次旅行开始时间为上午8点,最后一次旅行结束时间为24:00

所以我需要以下数据作为输出

  family    persons    trip    tour   start time   end time
     1      1        1        1         3         15:30
     1      1        5        2        20:00        8:30
     1      2        1        1        3:00        24:00
     1      3        1        1        8:00        24:00

因此,对于每个巡演,我们都有一行,因为您的
开始时间
结束时间
不是标准格式,而且它包含各种格式,我们首先需要将它们转换为标准格式。我们可以使用
lubridate::parse_date_time
通过指定列可以采用的各种格式来实现这一点。一次,我们可以按
家庭
人员
巡演
分组,并分别选择开始和结束时间的最小值和最大值

library(dplyr)

df %>%
    mutate_at(vars(starttime, endtime), 
            list(new = ~lubridate::parse_date_time(., c("%H:%M", "%H")))) %>%
  group_by(family, persons, tour) %>%
  summarise(starttime = starttime[which.min(starttime_new)], 
            endtime = endtime[which.max(endtime_new)])

#  family persons  tour starttime endtime
#   <int>   <int> <int> <fct>     <fct>  
#1      1       1     1 3         15:30  
#2      1       1     2 20:00     22:00  
#3      1       2     1 3:00      24:00  
#4      1       3     1 8:00      24:00  
库(dplyr)
df%>%
在(变量(开始时间、结束时间)处进行变异,
列表(新=~lubridate::解析日期时间(,c(“%H:%M”,“%H”)))%%>%
团体(家庭、个人、旅游)%>%
总结(starttime=starttime[which.min(starttime\u new)],
endtime=endtime[which.max(endtime\u new)])
#家庭成员旅游开始时间结束时间
#               
#1      1       1     1 3         15:30  
#2      1       1     2 20:00     22:00  
#3      1       2     1 3:00      24:00  
#4      1       3     1 8:00      24:00  
数据

df <- structure(list(family = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L), persons = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 
3L), trip = c(1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 1L, 2L), tour = c(1L, 
1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L), starttime = structure(c(7L, 
10L, 1L, 2L, 4L, 5L, 8L, 9L, 3L, 9L, 6L), .Label = c("13:30", 
"15:00", "17:00", "20:00", "22:00", "23:00", "3", "3:00", "8:00", 
"8:45"), class = "factor"), endtime = structure(c(10L, 1L, 2L, 
3L, 5L, 9L, 8L, 4L, 7L, 6L, 7L), .Label = c("13:30", "15", "15:30", 
"17:00", "22:00", "23:00", "24:00", "8:00", "8:30", "8:45"), class = 
"factor")), class = "data.frame", row.names = c(NA, -11L))
df