如何规范化和绘制data.table的列(直方图)?

时间:2018-11-28 10:39:08

标签: r histogram

我有一个名为Buffer_list的大型列表(20个元素,4.2Mb),我将其转换为如下所示的数据帧(现在为Buffer_norm):

#changing the List to a data frame with max for the biggest element
max.length <- max(sapply(Buffer_list, length))
List_norm <- lapply(Buffer_list, function(v) { c(v, rep(NA, max.length-length(v)))})
Buffer_norm <- as.data.frame(List_norm)
names(Buffer_norm) = c(1:20)

现在,每列的长度相同(57.443行),较短列表的NA相同。

当查看data.frame时,它告诉我所有值都是数字。尝试此操作时相同:

sapply(Buffer_norm, mode)
        1         2         3         4         5         6         7         8         9        10        11        12        13 
"numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" 
       14        15        16        17        18        19        20 
"numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" 
> sapply(Buffer_norm, class)
        1         2         3         4         5         6         7         8         9        10        11        12        13 
"numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" 
       14        15        16        17        18        19        20 
"numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" 

显示

时,这些值不是数字
plot(density(Buffer_norm))
  

density.default(Buffer_norm)中的错误:参数“ x”必须为数字   我想为y轴从0到1的每一列本身绘制data.frame的密度直方图。必须牢记将列表转换为data.frame所增加的NA!

我尝试遵循此规则:R Normalize then plot two histograms together in Rggplot2 histogram with density curve that sums to 1 没有任何成功!

1 个答案:

答案 0 :(得分:0)

您的Buffer_norm是一个数据集,您必须选择一个变量。请使用mtcars数据检查此示例。

str(mtcars)
plot(density(mtcars))
 Error in density.default(mtcars) : argument 'x' must be numeric
plot(density(mtcars[, 1]))

enter image description here