简体   繁体   English

如何绘制包含R中缺失值的向量的密度曲线?

[英]How to plot the density curve of a vector containing missing values in R?

I have a data set with some NA values (missing values). 我有一些NA值(缺失值)的数据集。

Because I need to plot some density curves from this data, I've created the following function: 因为我需要从这些数据绘制一些密度曲线,所以我创建了以下函数:

plotDistribution = function (x) {
    N = length(x)
    hist( x,col = "light blue",
          probability = TRUE)
    lines(density(x), col = "red", lwd = 3)
    rug(x)
}

It works just fine if x has no missing values but because my data do contain missing values I'm getting the following message: Error in density.default(x) : 'x' contains missing values . 如果x没有缺失值,它就可以正常工作,但是因为我的数据确实包含缺失值,所以我收到以下消息: density.default(x)中的错误:'x'包含缺失值

My question is: how to plot the density curve of the non-missing values in a data set? 我的问题是:如何在数据集中绘制非缺失值的密度曲线? How can I ignore the NA and plot the curve as if they don't exist? 如何忽略NA并绘制曲线,就好像它们不存在一样?

You can just take the non missing values of x in the function, such as: 您可以只在函数中获取x的非缺失值,例如:

plotDistribution = function (x) {
  NoMissing <- x[!is.na(x)]
  N = length(NoMissing)
  hist( NoMissing,col = "light blue",
        probability = TRUE)
  lines(density(NoMissing), col = "red", lwd = 3)
  rug(NoMissing)
}

It should work just adding na.omit() 只需添加na.omit()就可以工作

plotDistribution = function (x) {
  N = length(x)
  x <- na.omit(x)
  hist( x,col = "light blue",
        probability = TRUE)
  lines(density(x), col = "red", lwd = 3)
  rug(x)
  print(N-length(x))
}

This function can hide data bugs in many cases, so i added a line that print the number of ommited values. 在许多情况下,此功能可以隐藏数据错误,因此我添加了一行打印省略值的数量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM