gpt4 book ai didi

r - 如何在 tibbles 上取消对 which.min 的破坏

转载 作者:行者123 更新时间:2023-12-02 09:12:46 25 4
gpt4 key购买 nike

/R-newb。我的 which.min 调用在我的数据帧的一列上工作正常,但在加载 dplyr 时,which.min 不再工作。是因为 df 现在是 tibble 吗?有什么解决办法吗?

> which.min(Analytic_traces_raw[,1]) #********************WORKS FINE HERE**
[1] 1
> library(dplyr)

Attaching package: ‘dplyr’
The following objects are masked from ‘package:stats’:
filter, lag
The following objects are masked from ‘package:base’:
intersect, setdiff, setequal, union

> which.min(Analytic_traces_raw[,1]) #*********************NOW IT'S BROKEN**
Error in which.min(Analytic_traces_raw[, 1]) :
(list) object cannot be coerced to type 'double'

> head(Analytic_traces_raw[,1]) #**TYPE IS DBL, BUT TIBBLE TROUBLE**?
# A tibble: 6 x 1
`t_Pur23Mar18_TP7-24`
<dbl>
1 0.000417
2 0.00375
3 0.00708
4 0.0104
5 0.0138
6 0.0171

最佳答案

“破坏”的不是 which.min,而是 tibbles“破坏”了 R 中的默认行为。通常,对于数据帧,Analytic_traces_raw[,1] 将降低维度并成为向量。 tibbles 改变了这种行为,因此期望“正常”行为的 R 函数将会中断。尝试Analytic_traces_raw[[1]]:

library(dplyr)
which.min(mtcars[,1])
[1] 15
> mtcars_tbl <- as_tibble(mtcars)
> which.min(mtcars_tbl[,1])
Error in which.min(mtcars_tbl[, 1]) :
(list) object cannot be coerced to type 'double'
> which.min(mtcars_tbl[[1]])
[1] 15

关于r - 如何在 tibbles 上取消对 which.min 的破坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50300641/

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