I have a data set with some NA values (missing values).
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 .
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?
You can just take the non missing values of x in the function, such as:
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()
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.