gpt4 book ai didi

R:如何在处理时间时将字符转换为数值而不创建 NA 或 NaN?

转载 作者:行者123 更新时间:2023-12-05 01:51:19 24 4
gpt4 key购买 nike

我需要做什么:

我有一个数据框,我试图在 3 个实例中找到 ride_duration 的平均值(意思是“客户”的意思和 user_type 中“订阅者”的意思。

当运行 head() 时,这是我看到的。

head(all_trips)
# A tibble: 6 × 8
trip_id start_time end_time bike_id from_station_id to_station_id user_type ride_duration
<dbl> <chr> <chr> <dbl> <dbl> <dbl> <chr> <chr>
1 22081306 3/18/2019 22:36 3/18/2019 2… 4836 233 233 Customer 0:01:01
2 21965689 2/25/2019 19:44 2/25/2019 1… 5568 623 623 Customer 0:01:01
3 21763499 1/5/2019 7:49 1/5/2019 7:… 5905 45 45 Customer 0:01:01
4 21942654 2/20/2019 12:40 2/20/2019 1… 5940 3 3 Customer 0:01:01
5 21839414 1/18/2019 16:22 1/18/2019 1… 3925 424 424 Customer 0:01:03
6 22118075 3/23/2019 15:54 3/23/2019 1… 6025 34 34 Customer 0:01:04

尝试将 ride_duration 转换为数字时,它会将整列更改为 NA。当我尝试将 start_time 或 end_time 转换为数字时会发生同样的事情,这样我就可以使用 difftime() 来创建一个新的 ride_duration 列。

我尝试过的:

all_trips$ride_duration <- as.numeric(as.character(all_trips$ride_duration))
Warning message:
NAs introduced by coercion
> all_trips$ride_duration <- as.numeric(paste(all_trips$ride_duration))
Warning message:
NAs introduced by coercion
transform(all_trips, ride_duration= as.numeric(ride_duration))

上面的那个只是返回了表格的预览,其中 NA 全部下降了 ride_duration。

然后我尝试删除 : 符号,然后转换为数字。

all_trips$ride_duration <-gsub(":","",as.character(all_trips$ride_duration))
> head(all_trips)
# A tibble: 6 × 8
trip_id start_time end_time bike_id from_station_id to_station_id user_type ride_duration
<dbl> <chr> <chr> <dbl> <dbl> <dbl> <chr> <chr>
1 22081306 3/18/2019 22:36 3/18/2019 2… 4836 233 233 Customer 00101
2 21965689 2/25/2019 19:44 2/25/2019 1… 5568 623 623 Customer 00101
3 21763499 1/5/2019 7:49 1/5/2019 7:… 5905 45 45 Customer 00101
4 21942654 2/20/2019 12:40 2/20/2019 1… 5940 3 3 Customer 00101
5 21839414 1/18/2019 16:22 1/18/2019 1… 3925 424 424 Customer 00103
6 22118075 3/23/2019 15:54 3/23/2019 1… 6025 34 34 Customer 00104
> all_trips$ride_duration <- as.numeric(as.character(all_trips$ride_duration))
Warning message:
NAs introduced by coercion
> head(all_trips)
# A tibble: 6 × 8
trip_id start_time end_time bike_id from_station_id to_station_id user_type ride_duration
<dbl> <chr> <chr> <dbl> <dbl> <dbl> <chr> <dbl>
1 22081306 3/18/2019 22:36 3/18/2019 2… 4836 233 233 Customer 101
2 21965689 2/25/2019 19:44 2/25/2019 1… 5568 623 623 Customer 101
3 21763499 1/5/2019 7:49 1/5/2019 7:… 5905 45 45 Customer 101
4 21942654 2/20/2019 12:40 2/20/2019 1… 5940 3 3 Customer 101
5 21839414 1/18/2019 16:22 1/18/2019 1… 3925 424 424 Customer 103
6 22118075 3/23/2019 15:54 3/23/2019 1… 6025 34 34 Customer 104
> mean(all_trips$ride_duration)
[1] NA
> mean(all_trips$ride_duration, na.rm = TRUE)
[1] 2989.162
>

我仍然收到一条错误消息,显示其中的一些项目有 NA,但我很担心,因为在我转换为数字后,它从我的 HH:MM:SS 时间格式中删除了一些零,所以我担心它可能计算不正确.

我还尝试将我原来的 4 个数据帧 q1-4 转换为新列中的日期:

 q1$date <- as.Date(q1$start_time, "%m/%d/%Y %H:%M:%S")

但它返回 NA 的值

重复问题如何在不创建 NA 的情况下将 ride_duration 转换为数字?

还有一种方法可以转换回运行 as.numeric 之前的状态,而无需重新运行我所有的代码加载和绑定(bind)数据帧吗?

最佳答案

使用 data.table 中的 as.ITime:

as.numeric(data.table::as.ITime(times))
[1] 61 61 61 61 63 64

 times <- c('0:01:01', '0:01:01', '0:01:01', '0:01:01', '0:01:03', '0:01:04')

关于R:如何在处理时间时将字符转换为数值而不创建 NA 或 NaN?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72324802/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com