作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下 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"))
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
type_convert
.
最佳答案
library(lubridate)
阅读 df
没有任何改动(您的 dput
代码)。
将小时添加到分秒:
df$statistics.minutes <- paste0("00:", df$statistics.minutes)
df$statistics.minutes <- lubridate::hms(df$statistics.minutes)
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
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() %>%
... %>%
关于readr::type_convert 弄乱了时间列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53982826/
我是一名优秀的程序员,十分优秀!