Reputation: 30
I'm looking for a solution in R for merging pairs of rows into one.
Task | Starttime | Endtime |
---|---|---|
Programming | 13:35 | 13:50 |
Programming | 14:00 | 15:40 |
Production | 15:45 | 16:00 |
QualityInspection | 16:15 | 16:30 |
QualityInspection | 16:45 | 17:00 |
library(tidyverse)
df <- data.frame('Task' = c('Promgramming', 'Programming', 'Production', 'QualityInspection', 'QualityInspection'),
'Starttime' = c('13:35', '14:00', '15:45', '16:15', '16:45'),
'Endtime' = c('13:50', '15:40', '16:00', '16:30', '17:00'))
My desired table is the following:
Task | Starttime | Endtime |
---|---|---|
Programming | 13:35 | 15:40 |
Production | 15:45 | 16:00 |
QualityInspection | 16:15 | 17:00 |
Tasks always come in pairs of two or one.
How can I achieve this?
Upvotes: 0
Views: 61
Reputation: 887891
Using data.table
library(data.table)
setDT(df)[, .(Starttime = first(Starttime), Endtime = last(Endtime)), Task]
Upvotes: 0
Reputation: 389265
For each Task
you can extract first Starttime
and last EndTime
.
library(dplyr)
df %>%
group_by(Task) %>%
summarise(Starttime = first(Starttime),
Endtime = last(Endtime))
# Task Starttime Endtime
# <chr> <chr> <chr>
#1 Production 15:45 16:00
#2 Programming 13:35 15:40
#3 QualityInspection 16:15 17:00
data
df <- structure(list(Task = c("Programming", "Programming", "Production",
"QualityInspection", "QualityInspection"), Starttime = c("13:35",
"14:00", "15:45", "16:15", "16:45"), Endtime = c("13:50", "15:40",
"16:00", "16:30", "17:00")), class = "data.frame", row.names = c(NA, -5L))
Upvotes: 2