[R] error in density plot

David Winsemius dwinsemius at comcast.net
Thu Feb 3 20:59:53 CET 2011


On Feb 3, 2011, at 2:14 PM, Ben Bolker wrote:

> On 02/02/2011 09:29 AM, David Winsemius wrote:
>>
>> 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.
>>
>
>  na.omit() is (approximately) equivalent to x[is.na(x)] for vectors.
>  I just used it because I couldn't remember whether density() had an
> na.rm argument or not.

My concern was that it was "outside" the density function and I  
thought the density function would throw an error before it passed  
anything to na.omit().

plot(na.omit(density(c(1,1,1,1,1,NA,4,4,4,4)))  )
Error in density.default(c(1, 1, 1, 1, 1, NA, 4, 4, 4, 4)) :
   'x' contains missing values

plot(density(na.omit(c(1,1,1,1,1,NA,4,4,4,4))) )  # "works"


>
> Ben
>
>

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list