【问题标题】:readr::type_convert messes up time columnreadr::type_convert 弄乱了时间列
【发布时间】:2019-05-27 17:07:00
【问题描述】:

我有以下 R 数据框:

zed
# A tibble: 10 x 3
   jersey_number first_name statistics.minutes
   <chr>         <chr>      <chr>             
 1 20            Marques    8:20              
 2 53            Brennan    00:00             
 3 35            Marvin     40:00             
 4 50            Justin     00:00             
 5 14            Jordan     00:00             
 6 1             Trevon     31:00             
 7 15            Alex       2:00              
 8 51            Mike       00:00             
 9 12            Javin      17:00             
10 3             Grayson    38:00     

> dput(zed)
structure(list(jersey_number = c("20", "53", "35", "50", "14", 
"1", "15", "51", "12", "3"), first_name = c("Marques", "Brennan", 
"Marvin", "Justin", "Jordan", "Trevon", "Alex", "Mike", "Javin", 
"Grayson"), statistics.minutes = c("8:20", "00:00", "40:00", 
"00:00", "00:00", "31:00", "2:00", "00:00", "17:00", "38:00")), row.names = c(NA, 
-10L), class = c("tbl_df", "tbl", "data.frame"))

这是我从 API 接收数据的格式。所有列(大约有 100 个列)最初都属于 character 类。转换一切,我使用readr::type_convert(),但出现以下错误:

> zed %>% readr::type_convert()
Parsed with column specification:
cols(
  jersey_number = col_integer(),
  first_name = col_character(),
  statistics.minutes = col_time(format = "")
)
# A tibble: 10 x 3
   jersey_number first_name statistics.minutes
           <int> <chr>      <time>            
 1            20 Marques    08:20             
 2            53 Brennan    00:00             
 3            35 Marvin        NA             
 4            50 Justin     00:00             
 5            14 Jordan     00:00             
 6             1 Trevon        NA             
 7            15 Alex       02:00             
 8            51 Mike       00:00             
 9            12 Javin      17:00             
10             3 Grayson       NA 

与其抛出错误和搞乱转换,我希望此分钟列改为 class== 数字。如果一行显示此列的“8:20”,我希望将其简单地转换为 8.33。

关于我如何做到这一点的任何想法 - 最好是允许我继续使用 type_convert 的东西。

【问题讨论】:

标签: r data-manipulation readr


【解决方案1】:

library(lubridate)

阅读df,无需更改(您的dput 代码)。

将小时添加到分钟-秒:

df$statistics.minutes <- paste0("00:", df$statistics.minutes)

转换为时间类型:

df$statistics.minutes <- lubridate::hms(df$statistics.minutes)

除以 60:

period_to_seconds(df$statistics.minutes) / 60

结果:

 [1]  8.333333  0.000000 40.000000  0.000000  0.000000
 [6] 31.000000  2.000000  0.000000 17.000000 38.000000

如果需要,替换为 df

df$statistics.minutes <- period_to_seconds(df$statistics.minutes) / 60

[OP 的补充] :-)

我创建了以下辅助函数 - 基于此结果 - 所以我可以在不破坏管道链的情况下解决问题:

fixMinutes <- function(raw.data) {

  new.raw.data <- raw.data %>%
    dplyr::mutate(statistics.minutes = paste0("00:", statistics.minutes)) %>%
    dplyr::mutate(statistics.minutes = lubridate::hms(statistics.minutes)) %>%
    dplyr::mutate(statistics.minutes = lubridate::period_to_seconds(statistics.minutes) / 60)

  return(new.raw.data)
}

zed %>% 
  ... %>% 
  fixMinutes() %>%
  ... %>%

【讨论】:

  • 评论我自己的解决方案:现在了解更多,更好的解决方案类似于zed %&gt;% separate() 然后应用as.numeric,转换为分钟并合并两列。如果将来需要将这些句点添加到 POSIXct 元素中,最好使用 lubridate,否则这里不需要 lubridate。
【解决方案2】:

我唯一想到的就是先将有问题的列转换为数字,比如

(zed 
   ## split stats column in two, with names unlikely to clash w/ existing
   %>% tidyr::separate(statistics.minutes,c("tmp...mins","tmp...secs"))
   ## explicitly convert
   %>% dplyr::mutate(statistics.minutes=as.numeric(tmp...mins)+as.numeric(tmp...secs)/60)
   ## throw out the temp variables
   %>% dplyr::select(-starts_with("tmp..."))
   %>% readr::type_convert()
)

我不知道这是否满足您的“继续使用type_convert”标准。将自定义转换函数传递给type_convert 会更优雅,但我不知道该怎么做。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-16
    • 1970-01-01
    • 2017-11-09
    • 2017-03-01
    • 2016-03-27
    • 1970-01-01
    • 2021-06-09
    相关资源
    最近更新 更多