R - dplyr mutate with function仅检查第一行

时间:2018-05-06 21:39:37

标签: r function dplyr mutate

我有一个时间列表,我想将它们放入桶中。 即' 00 PM'进入下午7:00 - 凌晨12:00和#39;桶

功能如下:

bucket.start.time <- function(x) {
  if (strptime(x, format = "%I:%M %p") >= strptime("7:00", format = "%R") && 
      strptime(x, format = "%I:%M %p") < strptime("12:00", format = "%R")) {
    timebucket <- "7:00 AM - 11:59 AM"
  }
  if (strptime(x, format = "%I:%M %p") >= strptime("12:00", format = "%R") && 
      strptime(x, format = "%I:%M %p") <= strptime("16:30", format = "%R")) {
    timebucket <- "12:00 PM - 4:30 PM"
  }
  if (strptime(x, format = "%I:%M %p") > strptime("16:30", format = "%R") && 
      strptime(x, format = "%I:%M %p") < strptime("19:00", format = "%R")) {
    timebucket <- "4:31 PM - 6:59 PM"
  }
  if (strptime(x, format = "%I:%M %p") >= strptime("19:00", format = "%R") && 
      strptime(x, format = "%I:%M %p") < strptime("24:00", format = "%R")) {
    timebucket <- "7:00 PM - 12:00 AM"
  }
  return(timebucket)
} 

然后我使用mutated创建了第二列:

mutate(Appointments, Time.Bucket = bucket.start.time(Start.Time))

我发现,由于第一行的Start.Time为&#39; 12 PM&#39;,该功能仅在下午12:00 00时填充Time.Bucket列。 :下午30点&#39;。如何让mutate单独在每一行上应用我的函数,而不是将其应用到第一行并将结果复制到其余行?

2 个答案:

答案 0 :(得分:1)

尝试:

bucket.start.time =function(m){
  x=c("7:00 AM","12:00 PM","4:31 PM","7:00 PM","11:59 PM")
  x=strptime(x,format = "%I:%M %p")
  y=c("7:00 AM - 11:59 AM","12:00 PM - 4:30 PM",
      "4:31 PM - 6:59 PM","7:00 PM - 12:00 AM")

  cut(strptime(m,format = "%I:%M %p"),x,y)
}

mutate(Appointments, Time.Bucket = bucket.start.time(Start.Time))

答案 1 :(得分:0)

我不认为这是最好的答案,但我认为最重要的是输出矢量。我使用for循环来修改我的函数:

bucket.start.time2 <- function(x) {
  totalbuckets <- vector(mode="character", length=length(x))
  for (i in 1:length(x)) {  
    if (strptime(x[i], format = "%I:%M %p") >= strptime("7:00", format = "%R") && 
        strptime(x[i], format = "%I:%M %p") < strptime("12:00", format = "%R")) {
      timebucket <- "7:00 AM - 11:59 AM"
    }
    if (strptime(x[i], format = "%I:%M %p") >= strptime("12:00", format = "%R") && 
        strptime(x[i], format = "%I:%M %p") <= strptime("16:30", format = "%R")) {
      timebucket <- "12:00 PM - 4:30 PM"
    }
    if (strptime(x[i], format = "%I:%M %p") > strptime("16:30", format = "%R") && 
        strptime(x[i], format = "%I:%M %p") < strptime("19:00", format = "%R")) {
      timebucket <- "4:31 PM - 6:59 PM"
    }
    if (strptime(x[i], format = "%I:%M %p") >= strptime("19:00", format = "%R") && 
        strptime(x[i], format = "%I:%M %p") < strptime("24:00", format = "%R")) {
      timebucket <- "7:00 PM - 12:00 AM"
    }
    totalbuckets[i] <- timebucket
  }
  return(totalbuckets)
}

现在可以在以下声明中使用:

mutate(Appointments, Time.Bucket = bucket.start.time(Start.Time))