user1609391
user1609391

Reputation: 455

R Hist Function with Lattice Package

I am learning R. I am reading a book that has the below hist function which is basically removing 0 values and values >=1000 from the histogram.

Problem is I don't understand what the code is saying and the book does not explain it.

What are the conditions inside the () and why is !0 specified twice? Is there another way to write this code that is a little more intuitive? I am using the lattice package.

Your help is much appreciate!

hist(don$TGiving[don$TGiving!=0][don$TGiving[don$TGiving!=0]<=10000])

Upvotes: 0

Views: 195

Answers (2)

Ben Bolker
Ben Bolker

Reputation: 226532

If you do want to use lattice, you should use histogram() instead of hist(). subset() is useful too.

 set.seed(101)
 don <- data.frame(TGiving=round(rgamma(1000,shape=5,scale=100)))
 library(lattice)
 histogram(~TGiving,data=subset(don,TGiving!=0 & TGiving<1000))

Upvotes: 1

IRTFM
IRTFM

Reputation: 263411

This a pretty cluncky way of doing it. Perhaps it would be easier to see what was happening if you created a temporary variable with the first part of that expression which removes the values below 0 and then worked with it.

 temp <- don$TGiving[don$TGiving!=0]  # remove items below 0
 hist( temp[ temp  < 1000 ] )         # remove items above 1000  and then plot

Upvotes: 1

Related Questions