Plot a histogram without zero values in R

15,079

You can just use subsetting like this:

hist( a[ !a==0 ])

You can check it works like so:

table(is.na(c))
FALSE  TRUE 
 443    57 

length(a[!a==0])
[1] 443
Share:
15,079
Joschi
Author by

Joschi

Updated on June 09, 2022

Comments

  • Joschi
    Joschi about 2 years

    I'd like to exclude all zero values from a histogram. Until now to do so I created a new object and transformed all zero values to NAs but I hoped there would be some easier way without creating new objects.

    Example code:

    set.seed(45)
    a<-sample(0:10,500,replace=T)
    c<-ifelse(a!=0,a,NA)
    hist(c)