我有以下变量,我想将其转换为分钟,因此得到一个数字变量。
目前我正在做以下事情:
BrowserWindow.addDevToolsExtension('path/to/extension')
在“50分钟”的情况下,它转换完美,但在“1小时0分钟”的情况下转换为1分钟。
答案 0 :(得分:2)
require(lubridate)
time <- c("1 hour 10 mins", "1 hour 0 mins", "12 mins", "1 hour 0 mins",
"1 hour 4 mins", "1 hour 0 mins", "1 hour 0 mins", "1 hour 0 mins",
"1 hour 5 mins", "1 hour 0 mins", "34 mins", "1 hour 0 mins",
"50 mins", "1 hour 0 mins", "1 hour 0 mins", "1 hour 0 mins",
"1 hour 0 mins", "1 hour 0 mins", "1 hour 0 mins", "1 hour 0 mins",
"1 hour 0 mins", "1 hour 0 mins", "1 hour 34 mins", "1 hour 0 mins",
"43 mins", "1 hour 20 mins", "1 hour 0 mins", "1 hour 0 mins",
"1 hour 0 mins", "1 hour 30 mins", "1 hour 0 mins")
time <- gsub(" mins", "", time)
tmp <- grepl(" hour ", time)
time <- gsub("1 hour ", "", time) # If you sometimes have > 1 hour then use grep or regexpr here, or more gsubs if you want
time <- as.numeric(time)
time[tmp] <- time[tmp]+60
x <- ymd("1900-01-01") # or whenever
minute(x) <- time
x
[1] "1900-01-01 01:10:00 UTC" "1900-01-01 01:00:00 UTC" "1900-01-01 00:12:00 UTC" "1900-01-01 01:00:00 UTC" "1900-01-01 01:04:00 UTC" "1900-01-01 01:00:00 UTC"
[7] "1900-01-01 01:00:00 UTC" "1900-01-01 01:00:00 UTC" "1900-01-01 01:05:00 UTC" "1900-01-01 01:00:00 UTC" "1900-01-01 00:34:00 UTC" "1900-01-01 01:00:00 UTC"
[13] "1900-01-01 00:50:00 UTC" "1900-01-01 01:00:00 UTC" "1900-01-01 01:00:00 UTC" "1900-01-01 01:00:00 UTC" "1900-01-01 01:00:00 UTC" "1900-01-01 01:00:00 UTC"
[19] "1900-01-01 01:00:00 UTC" "1900-01-01 01:00:00 UTC" "1900-01-01 01:00:00 UTC" "1900-01-01 01:00:00 UTC" "1900-01-01 01:34:00 UTC" "1900-01-01 01:00:00 UTC"
[25] "1900-01-01 00:43:00 UTC" "1900-01-01 01:20:00 UTC" "1900-01-01 01:00:00 UTC" "1900-01-01 01:00:00 UTC" "1900-01-01 01:00:00 UTC" "1900-01-01 01:30:00 UTC"
[31] "1900-01-01 01:00:00 UTC"
或者如果您只想要原始分钟数,那么您当然已经在time
答案 1 :(得分:2)
这是一种方法:
library(chron)
times <- c("1 hour 10 mins", "1 hour 0 mins", "12 mins", "1 hour 0 mins",
"1 hour 4 mins", "1 hour 0 mins", "1 hour 0 mins", "1 hour 0 mins",
"1 hour 5 mins", "1 hour 0 mins", "34 mins", "1 hour 0 mins",
"50 mins", "1 hour 0 mins", "1 hour 0 mins", "1 hour 0 mins",
"1 hour 0 mins", "1 hour 0 mins", "1 hour 0 mins", "1 hour 0 mins",
"1 hour 0 mins", "1 hour 0 mins", "1 hour 34 mins", "1 hour 0 mins",
"43 mins", "1 hour 20 mins", "1 hour 0 mins", "1 hour 0 mins",
"1 hour 0 mins", "1 hour 30 mins", "1 hour 0 mins")
# Add "0 hour" when only minutes are present
times <- sub(pattern = "^(\\d+ mins)",replacement = "0 hour \\1", times)
# Change format simply to ease next step
times <- sub(pattern = "^(\\d+) hours? (\\d+) mins", replacement = "\\1\\:\\2", times)
# Convert to a real time vector
times.2 <- strptime(x = times, format = "%H:%M")
# Extract number of minutes
minutes <- 60*hours(times.2) + minutes(times.2)
> minutes
[1] 70 60 12 60 64 60 60 60 65 60 34 60 50 60 60 60 60 60 60 60 60 60 94
[24] 60 43 80 60 60 60 90 60
答案 2 :(得分:1)
另一种选择(类似逻辑但不同的方法):
library(lubridate)
x[!grepl("hour", x)] <- paste("0 hour", x[!grepl('hour', x)], sep = " ")
hour(strptime(x, "%H hour %M mins"))*60 + minute(strptime(x, "%H hour %M mins"))
# [1] 70 60 12 60
数据强>:
c("1 hour 10 mins", "1 hour 0 mins", "0 hour 12 mins", "1 hour 0 mins"
)