Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
873 views
in Technique[技术] by (71.8m points)

r - Creating a histogram using aggregated data

Embarrassingly simple question...

I'm new to R and I can't seem to wrap my head around this for some reason. I have a CSV file which looks something like this:

Bin,Number
1363,5
1028,4
1303,3
1467,1
1242,3
1415,5
..
.

The bin size is 1, with a range of 1000-1500. I have read my CSV file in, everything seems to be ok there, but I just cannot produce a simple histogram. I have tried simply using a barplot, but the data is not numerically ordered, so will not produce the output I need. Using data such as this, how can I produce a histogram in R?

Once I have a simple histogram, I'm sure I'll be able to play around with it and format it nicely.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Because the hist function does the counting of items in each bin, you need to 'explode' your 'already counted' data, for example by using rep. Then you can use hist on the resulting vector.

with(df, hist(rep(x = Bin, times = Number)))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...