如何根据部分字符串变量来重新组合因子水平?

如果后续点之间的时间差大于某个阈值,我将拆分一些数据,并重命名发生拆分的ID

# prep the data
ID = c(rep("A",5),rep("B",5))
DateTime = c("2014-09-25 08:39:45","2014-09-25 08:39:48","2014-09-25 08:40:44","2014-09-25 09:04:00","2014-09-25 09:04:10","2014-09-25 08:33:32","2014-09-25 08:34:41","2014-09-25 08:35:24","2014-09-25 09:04:09")
df = data.frame(ID,DateTime,stringsAsFactors = FALSE)
df$DateTime<-as.POSIXct(df$DateTime,tz = "UTC")

# split if the time difference is greater than 100 and rename the IDs

library(dplyr)
df %>%
  group_by(ID) %>%
  mutate(timeDiff = c(NA,difftime(tail(DateTime,-1),head(DateTime,units="sec"))) %>%
  mutate(newID = paste(ID,cumsum(!is.na(timeDiff) & timeDiff > 100),sep = "_")) %>%
  ungroup()

此后,我基于newID列运行一些函数数据。

但是,在此之后,我想再次将数据缝合在一起。因此,A_0和A_1会像A一样粘在一起,而B也会类似。

我只需要能在进行针迹时识别_之前的因子水平的东西,但不确定如何。

aishine 回答:如何根据部分字符串变量来重新组合因子水平?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3136124.html

大家都在问