[R] error in density plot

Ben Bolker bbolker at gmail.com
Thu Feb 3 20:14:12 CET 2011


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.

 Ben



More information about the R-help mailing list