使用 R 在表中交换 start_time 与 end_time(反之亦然)

问题描述 投票:0回答:1

我正在处理一个自行车共享数据集,在其原始数据中,start_time 已与 end_time 一起键入,反之亦然。 start time is later than end time

有没有办法可以将两列交换为受影响的行,或者有没有办法在使用 start_time - end_time 时获得绝对结果?

这就是我获取骑行持续时间(开始时间 - 结束时间)的方法:

#new column to show ride duration
# step 1 change type to what we want dttm
Q4_2019 <- Q4_2019 %>% 
  mutate(start_time = as_datetime(start_time)) %>% 
  mutate (end_time = as_datetime(end_time))
# step 2 create new column showing ride_duration
Q4_2019 <- Q4_2019 %>% 
  mutate(ride_duration = end_time - start_time)
df <- structure(list(trip_id = c(25625850, 25625849, 25625851, 25625843, 
25625841, 25625838, 25625830, 25625839, 25241500, 25293548, 25380297, 
25382592, 25425647, 25432182, 25447046, 25535424, 25539306), 
    start_time = structure(c(1572746268, 1572746133, 1572746297, 
    1572745919, 1572745831, 1572745669, 1572745401, 1572745684, 
    1569960882, 1570290715, 1570747626, 1570782378, 1571127078, 
    1571158556, 1571248410, 1571846602, 1571854341), class = c("POSIXct", 
    "POSIXt"), tzone = "UTC"), end_time = structure(c(1572742886, 
    1572742912, 1572743307, 1572742982, 1572743065, 1572742960, 
    1572743396, 1572743808, 1569960943, 1570290776, 1570747687, 
    1570782439, 1571127139, 1571158617, 1571248471, 1571846663, 
    1571854402), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
    bikeid = c(217, 5059, 6133, 2920, 4179, 964, 4141, 2214, 
    6029, 2081, 5397, 976, 6130, 3819, 1725, 6372, 6058), tripduration = c(5310, 
    379, 609, 663, 834, 891, 1594, 1724, 61, 61, 61, 61, 61, 
    61, 61, 61, 61), from_station_id = c(340, 109, 301, 460, 
    298, 229, 632, 131, 293, 76, 328, 308, 664, 174, 77, 91, 
    253)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-17L))
r lubridate swap mutate absolute-value
1个回答
0
投票
library(tidyverse)

# using mutate
df %>%
  mutate(diff = abs(difftime(start_time, end_time)),
         start_time = if_else(start_time > end_time, end_time, start_time),
         end_time = start_time + diff) %>%
  select(-diff)

# using transform (credit to G. Grothendieck)
df %>%
  transform(start_time = as_datetime(ifelse(start_time > end_time, end_time, start_time)),
            end_time = start_time + abs(difftime(start_time, end_time)))
© www.soinside.com 2019 - 2024. All rights reserved.