确定双列 r 的中位数

determine median of double column r

我有以下数据集

> temp6
# A tibble: 120 x 1
      Arithmetic Mean
            <dbl>
 1           0.96
 2           2.09
 3           0.57
 4           0.66
 5           0.92
 6           0.60
 7           0.40
 8           0.42
 9           0.27
10           0.47
# ... with 110 more rows

我非常需要这个数据列的中位数,但显然当我尝试

median(temp6, na.rm=TRUE)

我收到此错误消息:

Error in median.default(temp6, na.rm = TRUE) : need numeric data

如果我尝试将此数据转换为 'numeric',那也不起作用

as.numeric(temp6, na.rm=TRUE)

as.numeric(as.character(temp6)

给出:

Error: (list) object cannot be coerced to type 'double'

Warning message:
NAs introduced by coercion 

分别。我已经做了足够的研究,知道这两个过程都不​​起作用,但我还没有找到任何类型的变通方法来找到这些数据点的中值。有什么办法可以做到这一点吗?

根据?median

median(x, na.rm = FALSE, ...)

哪里

x an object for which a method has been defined, or a numeric vector containing the values whose median is to be computed.

如果是 data.frame,则可以用 temp6[,1] 转换为 vector。因为它是 tibble,所以我们需要 [[。假设,我们使用 [

进行提取
temp6[,1]
# A tibble: 10 x 1
#   `Arithmetic Mean`
#               <dbl>
# 1              0.96
# 2              2.09
# 3              0.57
# 4              0.66
# 5              0.92
# 6              0.60
# 7              0.40
# 8              0.42
# 9              0.27
#10              0.47

它仍然是 tibble,其中使用 [[

temp6[[1]]
#[1] 0.96 2.09 0.57 0.66 0.92 0.60 0.40 0.42 0.27 0.47

它被转换成一个vector

is.vector(temp6[[1]])
#[1] TRUE

现在,我们可以得到 median

median(temp6[[1]], na.rm = TRUE)
#[1] 0.585

或使用$

median(temp6$`Arithmetic Mean`, na.rm = TRUE)
#[1] 0.585

数据

temp6 <- structure(list(`Arithmetic Mean` = c(0.96, 2.09, 0.57, 0.66, 
 0.92, 0.6, 0.4, 0.42, 0.27, 0.47)), .Names = "Arithmetic Mean", row.names = c("1", 
 "2", "3", "4", "5", "6", "7", "8", "9", "10"), class = c("tbl_df", 
"tbl", "data.frame"))

dplyr::summarise 是另一种选择。

library(dplyr)
temp6 %>% 
  summarise(Median = median(`Arithmetic Mean`, na.rm = TRUE))