简体   繁体   中英

Program R create histogram of data from specified levels of multiple factors

I want to create histograms from selected data in a larger file. Example data set structure as follows:

Column headers(values)
Trip (1,2,3,4)
Site (1,2)
Year (2013, 2104)
Amount (values range from 1 - 100)

For example, what code do I use to plot a histogram of values from Amount of only data from Trip 1, Site 1, Year 2014 ?

I got as far as hist(Amount[Trip=="1"]) , but I don't know how to further specify which data I want it to use.

You probably are looking for & , which ANDs logical vectors componentwise:

> nn <- 100
> Trip <- sample(1:4,nn,T)
> Site <- sample(1:2,nn,T)
> Amount <- runif(nn)
> hist(Amount[Trip==1 & Site==1])

Don't confuse & with && ! Look at ?"&&" for more info.

You may want to read "An Introduction to R", which should be available as PDF under the "Help" menu entry of your R GUI.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM