如何在具有多参数功能的数据集列中使用套用或套用

时间:2019-05-13 19:43:54

标签: r function lapply

我的数据集称为数据,我有一列称为时间的列,其中包含以mm:ss格式表示的时间。我还写了一个函数functime(var1,var2)。

我最终希望使用apply或vapply并将var2设置为常量(假设var2 = 6),并将var1设置为列data $ time的每个值。

类似的东西:

如果

data$time <- c("10:10","11:00", "09:30"), when I do vapply(), I would like to get a 

data$output <-c(functime(data$time[1],6),functime(data$time[2],6),functime(data$time[3],6))

在此示例中与

相同
data$output <- c(functime("10:10",6),functime("11:00",6),functime("09:30",6))

我la脚的尝试是这样的:

vapply(data$time,functime,var2 = 6,FUN.VALUE = 1)

vapply的文档说它应该是:vapply(x,fun,fun.value)

我对如何“说要vapply”感到困惑,我想将data $ time列的所有行作为其第一个参数,并将第二个固定的参数定义为6。

最终,我想使用mutate将data $ output添加到原始数据集中。

编辑:(包括数据和功能行)

data$id <- c(9,6,5763,4)

data$time <- c("5:06","5:06","5:11","5:08")

data$city <-c("Kyle","Oklahoma","Monterey","Austin")

函数是:

    calctime <- function(racePace, raceDistance){

  # racePace is the per unit pace in mm:ss - character
  # raceDistance is the total race distance - numeric
  # Pace and race distance must use same units (km or mi or whatever)

  # Seconds to character time function
  CharMinSec <- function(sec){
    outMin <- floor(sec/60)
   outSec <- ((sec/60)-outMin)*60
    if(outSec==0 | round(outSec)<10){
      outChar <- paste0(outMin,":0",round(outSec))
    } else {
      outChar <- paste(outMin,round(outSec),sep=":")
    }
    outChar
  }

  paceMinSec <- as.numeric(strsplit(racePace,':')[[1]])
  paceSec <- paceMinSec[1]*60+ paceMinSec[2]
  raceMin <- floor(paceSec*raceDistance/60)
  raceSec <- ((paceSec*raceDistance/60)-raceMin)*60
  raceTime <- CharMinSec(raceMin*60+raceSec)
  list(Seconds=raceSec)
}

# Example of 4:15/km for a half-marathon


calctime("4:15",21.097494)

2 个答案:

答案 0 :(得分:2)


calcTime <- function(pace,distance){ 
  return (lubridate::period_to_seconds(lubridate::ms(pace)) * distance)
}


pace <- c("10:10","11:00", "09:30")
vapply(pace,calcTime,6,FUN.VALUE = 1)

## 10:10 11:00 09:30 
## 36960 39960 34560 

d <- tibble::as_tibble(list(pace = pace))
dplyr::mutate(d, raceSeconds = calcTime(pace,6))

## A tibble: 3 x 2
##    pace   raceSeconds
##    <chr>  <dbl>
##  1 10:10  36960
##  2 11:00  39960
##  3 09:30  34560

答案 1 :(得分:1)

我不得不更改2件事,但是您的vapply通话是正确的。

在函数中,我更改了最后一行,因此它返回一个值,而不是带有一个值的列表

calctime <- function(racePace, raceDistance){

  # racePace is the per unit pace in mm:ss - character
  # raceDistance is the total race distance - numeric
  # Pace and race distance must use same units (km or mi or whatever)

  # Seconds to character time function
  CharMinSec <- function(sec){
    outMin <- floor(sec/60)
    outSec <- ((sec/60)-outMin)*60
    if(outSec==0 | round(outSec)<10){
      outChar <- paste0(outMin,":0",round(outSec))
    } else {
      outChar <- paste(outMin,round(outSec),sep=":")
    }
    outChar
  }

  paceMinSec <- as.numeric(strsplit(racePace,':')[[1]])
  paceSec <- paceMinSec[1]*60+ paceMinSec[2]
  raceMin <- floor(paceSec*raceDistance/60)
  raceSec <- ((paceSec*raceDistance/60)-raceMin)*60
  raceTime <- CharMinSec(raceMin*60+raceSec)
  raceSec
}

现在列表返回一个值,vapply()有效,但是在我的情况下,我不得不强制将time列设置为字符

data = data.frame(
  id = c(9,6,5763,4),
  time = c("5:06","5:06","5:11","5:08"),
  city = c("Kyle","Oklahoma","Monterey","Austin")
)

data$time = as.character(data$time)

data$output = vapply(data$time,calctime,raceDistance = 6,FUN.VALUE=1) #works fine