将 "hour:minute" 字符列转换为分钟的数字列(该列具有 NA 值)

Convert "hour:minute" character column to numeric column of minutes (column has NA values)

我在 r 中有一个名为 ipaq_2 的字符列,类似于“02:00”(小时:分钟)。该列也有一些 NA 值。我需要将此列转换为显示分钟的数字列(在上面的示例中:120),同时用平均值替换 NA 值。

您可以使用 lubridate::hm 将字符列转换为小时和分钟,然后将值转换为分钟。然后,我们可以用分钟列的平均值替换任何 NA 值。

library(lubridate)
df <- data.frame(ipaq_2 = c("1:00", "0:45", "0:30", "1:30", NA))

df$ipaq_2 <- hm(df$ipaq_2, quiet = TRUE)        # format to 'hours:minutes:seconds'

df$minutes <- hour(df$ipaq_2)*60 + minute(df$ipaq_2)

df$minutes[is.na(df$minutes)] <- mean(df$minutes, na.rm = TRUE)

或者另一种可能性(感谢@Ben):

df$minutes <- as.numeric(hm(df$ipaq_2, quiet = T))/60
df$minutes[is.na(df$minutes)] <- mean(df$minutes, na.rm = TRUE)

tidyverse:

library(tidyverse)
library(lubridate)

df %>% 
  mutate(ipaq_2 = hm(ipaq_2, quiet = TRUE),
         minutes = hour(ipaq_2)*60 + minute(ipaq_2),
         minutes = ifelse(is.na(minutes), mean(minutes, na.rm = TRUE), minutes))

# Or using the alternative above:
# df %>%
#   mutate(minutes = as.numeric(hm(ipaq_2, quiet = T))/60,
#          minutes = ifelse(is.na(minutes), mean(minutes, na.rm = TRUE), minutes))

输出

     ipaq_2 minutes
1  1H 0M 0S   60.00
2    45M 0S   45.00
3    30M 0S   30.00
4 1H 30M 0S   90.00
5      <NA>   56.25