在 R 中计算统计汇总时获取'NA'

  • 本文关键字:获取 NA 计算 统计 r
  • 更新时间 :
  • 英文 :


我试图将列从因子转换为数字,这样我就可以运行统计摘要(平均值,中位数,最大值,最小值)。然而,当我为本专栏运行任何统计数据时,我总是得到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.characterhms类转换为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

最新更新