中位数:需要数字数据(Median : need numeric data)

编程入门 行业动态 更新时间:2024-10-26 13:26:20
中位数:需要数字数据(Median : need numeric data)

为什么常规子集化可能无效? 我想从data.table中计算中位数:

> median(DT.UsersA3[,1]) Error in median.default(DT.UsersA3[, 1]) : need numeric data

它是数字,但:

> class(DT.UsersA3$Timedif) [1] "numeric"

随着虹膜数据框架的运行:

> median(iris[,3]) [1] 4.35

词汇子集也适用于...

> median(DT.UsersA3$Timedif) [1] 422.046

Why regular subsetting might not work? I wanted to calculate median from a data.table:

> median(DT.UsersA3[,1]) Error in median.default(DT.UsersA3[, 1]) : need numeric data

it is numeric though:

> class(DT.UsersA3$Timedif) [1] "numeric"

With iris data frame it works:

> median(iris[,3]) [1] 4.35

And lexical subsetting also works...

> median(DT.UsersA3$Timedif) [1] 422.046

最满意答案

这是iris的例子:

library(data.table) Iris <- data.table(iris) median(Iris[,1]) # Error in median.default(Iris[, 1]) : need numeric data Iris[,1] # is a data.table Iris[,Sepal.Length] # is a vector

要使用列的编号,您可以按照Dason的评论:

Iris[[1]] # would return a vector median(Iris[[1]]) # works

Here is the example with iris:

library(data.table) Iris <- data.table(iris) median(Iris[,1]) # Error in median.default(Iris[, 1]) : need numeric data Iris[,1] # is a data.table Iris[,Sepal.Length] # is a vector

To use the number of the column you can follow the comment from Dason:

Iris[[1]] # would return a vector median(Iris[[1]]) # works

更多推荐

本文发布于:2023-08-02 20:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1381532.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中位数   数字   数据   data   numeric

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!