- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要做什么:
我有一个数据框,我试图在 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/
在 R Language Definition 中,对NA值进行了简要描述,其中一部分说 ... In particular, FALSE & NA is FALSE, TRUE | NA is TR
我对 R 还很陌生,目前遇到一个问题,数据如下所示: ID h1 h2 h3 h4 h5 h6 h7 h8
我有一个 csv包含国家名称及其 ISO 代码的文件。这是它的样子: "Name","Code" "Afghanistan","AF" "Albania","AL" "Algeria","DZ" "N
我想用 dplyr 解决以下问题。最好与窗口功能之一一起使用。我有一个包含房屋和购买价格的数据框。下面是一个例子: houseID year price 1 19
在data.frame(或data.table)中,我想用最接近的先前非NA值“向前填充”NA。一个使用向量(而不是 data.frame)的简单示例如下: > y yy [1] NA NA NA
这是一个示例数据框: > df = data.frame(rep(seq(0, 120, length.out=6), times = 2), c(sample(1:50, 4), + NA, NA,
我有一个包含条目的数据框;似乎这些值不被视为 NA,因为 is.na 返回 FALSE。我想将这些值转换为 NA 但找不到方法。 最佳答案 使用 dfr[dfr==""]=NA哪里dfr是你的数据框。
我有一个示例表,其中包含一些但不是全部 NA需要替换的值。 > dat id message index 1 1 1 2 1 foo 2 3 1
在 R 中,如果从 NA 中减去一个数字,它将返回 NA: > x NA - x [1] NA 但是如果你尝试从 NA 中减去一个日期,它会返回一个错误: > x NA - x Error in
这个问题在这里已经有了答案: Logical operators (AND, OR) with NA, TRUE and FALSE (2 个答案) 关闭 4 年前。 为什么在 R 中会这样? >
我有一个看起来像这样的数据框: SampleNo Lab1 Lab2 Lab3 lab4 lab5 lab6 lab7 lab8 lab9 lab10 1 59
我有一个按“id”分组的数据框和一个包含缺失值的变量“age”,NA。 在每个“id”中,我想替换“age”的缺失值,但只“填充”之前 第一个 非NA 值。 data % group_by(id) %
我有如下所示的数据框: df df id value v1 v2 v3 1 1 351 NA 1 0 2 2 585 0 1 1 3 3 321 NA 0 1 4
所以我有一个数据集,只需查看它,数据集中就有明显的 NA。 > dput(bmi.cig) structure(list(MSI.subset.BMI = structure(c(4L, 4L, 4
我有两个 30m x 30m 的光栅文件,我想从中采样点。在采样之前,我想从图像中移除模糊区域。我求助于 R 和 Hijman 的 Raster 包来完成这项任务。 使用 drawPoly(sp=TR
我有以下时间序列 > y y[c(1,2,5,9,10)] y [,1] 2011-09-04 NA 2011-09-05 NA 2011-09-06 3 201
这个问题在这里已经有了答案: Replace missing values (NA) with most recent non-NA by group (7 个回答) 5年前关闭。 我有一个 DF 个
我想向我的数据框中添加一个新变量 (N_notNAs),它定义了其他任何变量是否为 NA。 x y z N_notNAs 2 3 NA NA NA 1 3 NA 2
我有一个名为 SMOKE 的因子,级别为“Y”和“N”。缺失值被替换为 NA(从初始级别“NULL”开始)。然而,当我查看这个因素时,我得到这样的结果: head(SMOKE) # N N Y Y
假设我有以下 data.frame: t<-c(1,1,2,4,5,4) u<-c(1,3,4,5,4,2) v<-c(2,3,4,5,NA,2) w<-c(NA,3,4,5,2,3) x<-c(2,
我是一名优秀的程序员,十分优秀!