R-尝试获取每行之间的时间差(顺序位置)?
我有这个(它不允许我在这里展示图片)。我试图获得每个按组件名称分组的连续位置之间的时间差。所以基本上,我想看看每个组件在某个位置花费了多长时间,然后想得到任何组件在某个位置花费的平均时间,然后根据位置和组件类型。我最初试图分散数据,以便位置是键,乘以值,然后获得列之间的差异,但每个组件类型都有不同的位置,因此不起作用R-尝试获取每行之间的时间差(顺序位置)?,r,dplyr,average,R,Dplyr,Average,我有这个(它不允许我在这里展示图片)。我试图获得每个按组件名称分组的连续位置之间的时间差。所以基本上,我想看看每个组件在某个位置花费了多长时间,然后想得到任何组件在某个位置花费的平均时间,然后根据位置和组件类型。我最初试图分散数据,以便位置是键,乘以值,然后获得列之间的差异,但每个组件类型都有不同的位置,因此不起作用 comps <- structure(list(component_name = c("COMPONENT000000001", "COMPONENT0000000
comps <- structure(list(component_name = c("COMPONENT000000001",
"COMPONENT000000001",
"COMPONENT000000001", "COMPONENT000000001", "COMPONENT000000001",
"COMPONENT000000001", "COMPONENT000000002", "COMPONENT000000002",
"COMPONENT000000002", "COMPONENT000000002", "COMPONENT000000002",
"COMPONENT000000002", "COMPONENT000000002"), component_type =
structure(c(4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("component_0",
"component_1", "component_2", "component_3"), class = "factor"),
location = structure(c(6L, 2L, 14L, 1L, 1L, 4L, 6L, 2L, 14L,
14L, 1L, 1L, 4L), .Label = c("29MSJ_03_01", "5YU1V_01_02",
"7EFLP_03_02", "assembly room", "B57X3_03_00", "GH9CV_00_03",
"HUX1L_02_02", "JX3UO_01_01", "MRX5B_01_00", "TG6IA_00_02",
"VUFVH_00_00", "YBSFJ_00_01", "ZAENM_02_01", "ZZU3X_02_00"
), class = "factor"), times = structure(c(1514764800, 1514771683,
1514784872, 1514794911, 1514806504, 1514820010, 1514764800,
1514776184, 1514789862, 1514794911, 1514806046, 1514831050,
1514843151), class = c("POSIXct", "POSIXt"), tzone = "America/New_York")), .Names = c("component_name",
"component_type", "location", "times"), row.names = c(NA, 13L
), class = "data.frame")
loc_diff <- comps %>%
group_by(., type, location) %>%
mutate(., diff = as.numeric(difftime(max(times), min(times))))
ld <- loc_diff %>%
group_by(., location) %>%
summarise(., avg = mean(diff))
comps%
变异(,diff=as.numeric(difftime(最大(次),最小(次)))
ld%
分组依据(%,位置)%>%
总结(,平均值=平均值(差异))
这是我最初尝试做的,但后来它给了我一个数据帧,所有的平均值都差不多,我不认为这是正确的,基于我所做的其他探索。非常感谢您的帮助。谢谢
另外,我不确定我是否应该在这方面做一个tsa,但我是新来的,我仍在努力让它发挥作用 首先可以通过对
component\u name
进行分组,然后计算下一行和当前行的时间
之间的差值,计算组件在每个位置花费的时间。可以使用difftime
查找两次之间的差异(以秒为单位)
库(dplyr)
图书馆(tidyr)
图书馆(lubridate)
#首先获取每个组件在某个位置花费的时间
公司时间支出百分比
分组依据(组件名称)%>%
变异(timespendaltocation=difftime(前置时间、时间、单位=“秒”))
#按“组件名称”分组,以查找每个组件在某个位置上花费的平均时间
组件时间SPENDLOCATOIN%%>%group(组件名称)%%>%
总结(avgTimeComponentAtLocation=平均值(TimePendAtLocation,na.rm=真))
#组件名称avgTimeComponentAtLocation
#
#1部件000000001 11042
#2部件000000002 13058.5
#按组件类型和位置列出的在位置上花费的平均时间
组件时间SPENDLOCATOIN%%>%group(组件类型、位置)%%
总结(avgTimeComponentTypeAtLocation=平均值(timeSpendAtLocation,na.rm=TRUE))
##tibble:5 x 3
##组:组件_类型[?]
#组件类型位置avgTimeComponentTypeAtLocation
#
#1部件3 29MSJ_03_01 15551
#2部件3 5YU1V\U 01\U 02 13433.5
#3组件3会议室NaN
#4部件3 GH9CV_00_03 9133.5
#5部件3 ZZU3X部件02部件00 8741
请dput(comps)
并作为问题的一部分分享输出。这样会更容易。@MKR这就是你的意思吗?是的,谢谢。我现在可以做了。
library(dplyr)
library(tidyr)
library(lubridate)
#First get the time spend by each component at a location
comps_timesSpendAtLocatoin <- comps %>%
group_by(component_name) %>%
mutate(timeSpendAtLocation = difftime(lead(times),times, units = "secs"))
#Group_by 'component_name' to find average time spend on a location by each component
comps_timesSpendAtLocatoin %>% group_by(component_name) %>%
summarise(avgTimeComponentAtLocation = mean(timeSpendAtLocation, na.rm = TRUE))
# component_name avgTimeComponentAtLocation
# <chr> <time>
# 1 COMPONENT000000001 11042
# 2 COMPONENT000000002 13058.5
#Average time spend on a location by component_type and location
comps_timesSpendAtLocatoin %>% group_by(component_type, location) %>%
summarise(avgTimeComponentTypeAtLocation = mean(timeSpendAtLocation, na.rm = TRUE))
# # A tibble: 5 x 3
# # Groups: component_type [?]
# component_type location avgTimeComponentTypeAtLocation
# <fctr> <fctr> <time>
# 1 component_3 29MSJ_03_01 15551
# 2 component_3 5YU1V_01_02 13433.5
# 3 component_3 assembly room NaN
# 4 component_3 GH9CV_00_03 9133.5
# 5 component_3 ZZU3X_02_00 8741