[R] error in density plot

David Winsemius dwinsemius at comcast.net
Wed Feb 2 15:29:02 CET 2011


On Feb 2, 2011, at 8:22 AM, Ben Bolker wrote:

> Ramya <ramya.victory <at> gmail.com> writes:
>
>> I have the code for the density plot
>> j <- 8
>> plot(density(diff_in_sample[,1]), main = list.files()[j])
>> for(i in 1:25){
>> lines(density(diff_in_sample[,i]))
>> }
>>
>> This gives me an error
>> Error in density.default(diff_in_sample[, i]) :
>>  'x' contains missing values
>
> [snip]
>
> ?NA
>
> lines(na.omit(density(diff_in_sample[,i])))

I don't know if that will work, but since Bolker wrtote it,  it's got  
a reasonable probability of being correct code. I generally use the  
na.rm argument inside the density function rather than na.omit wrapped  
around it.

lines(density(diff_in_sample[,i], na.rm=TRUE))

Although if there are a lot of NA's then the registration of the lines  
results are going to be way off from the original plot axis.

-- 
David.


>
> Consider replacing the loop by apply() [no efficiency
> gain, but more compact code]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list