我试图将列从因子转换为数字,这样我就可以运行统计摘要(平均值,中位数,最大值,最小值)。然而,当我为本专栏运行任何统计数据时,我总是得到NA。在运行任何转换代码之前,如果我运行str()
,
类型的hms num[1:5860776] 00:40:56 01:08:32 00:07:19…
我尝试了以下代码
all_trips$ride_length <- as.numeric(as.character(all_trips$ride_length))
和is.numeric(all_trips$ride_length)
读TRUE
当我运行str()
时,我得到
num [1:58 . 60] NA NA NA NA NA NA NA NA ....
任何帮助都是感激的。
as.character
从hms
类转换为character
类(其中存储模式为numeric
),
> mode(hms::as_hms("12:34:56"))
[1] "numeric"
> typeof(hms::as_hms("12:34:56"))
[1] "double"
> class(hms::as_hms("12:34:56"))
[1] "hms" "difftime"
,因此当我们将as.character
强制转换为数字as.numeric
时,它返回NA,因为有字符(:
)
library(magrittr)
hms::as_hms("12:34:56") %>%
as.character %>%
as.numeric
[1] NA
hms::as_hms("12:34:56") %>%
as.numeric
[1] 45296