【发布时间】:2021-01-21 06:43:28
【问题描述】:
A 有一个带有代表小时和分钟的字符串的列的 tibble。 我想整理该列并将元素转换为仅代表分钟的整数。
这些字符串可以是以下形式之一:
- “5”(表示 5 分钟)
- “XX min”(表示 xx 分钟)
- “X Std”(表示 x 小时)
- “X Std. YY min”(表示 x 小时和 yy 分钟)
我写了一个函数把这些字符串转换成分钟。
- “5”应该变成 5。
- “45 分钟”应变为 45。
- “2 标准”应变为 120。
- “1 Std. 30 min”应该变成 90。
这是函数的样子:
convert_ZA_time <- function(string) {
if (nchar(string) == 1) {
result <- as.integer(string)
}
else if (endsWith(string, " Std")) {
result <- as.integer(substring(string, 1, 1)) * 60
}
else if (endsWith(string, " min") && nchar(string) == 6) {
result <- as.integer(substring(string, 1, 2))
}
else if (endsWith(string, " min") && nchar(string) > 6) {
hour <- as.integer(gsub(" Std.*", "", string, perl = TRUE))
minute_plus <- gsub("^\\d+ Std. ", "", string, perl = TRUE)
minute <- as.integer(gsub(" min$", "", minute_plus))
result <- hour * 60 + minute
}
else {result <- NA}
return(result)
}
用字符串测试它工作得很好:
convert_ZA_time("2 Std. 50 min")
# prints [1] 170
但是当我尝试在 tidyverse mutate 函数中使用此函数时,我收到以下错误:
df <- tibble(datestr = c("5", "45 min", "1 Std", "2 Std. 30 min"))
df2 <- df %>% mutate(minutes = convert_ZA_time(datestr))
# throws error: the condition has length > 1 and only the first element will be used
如何更改我的函数才能在 mutate 中正确使用它?
附:据我了解:mutate 获取每个“datestr”并将其放入函数“convert_ZA_time”中。但显然 mutate 将向量放入函数中?
感谢您的帮助!
【问题讨论】: