开发者

How to modify table and create bins for histogram representation in R

开发者 https://www.devze.com 2023-03-18 04:50 出处:网络
I am a bit new to R and I was experimenting with ggplot. I have a table like this: table http://dl.dropbox.com/u/43697/temporary/data_frame.jpg

I am a bit new to R and I was experimenting with ggplot.

I have a table like this:

table http://dl.dropbox.com/u/43697/temporary/data_frame.jpg

I would like to modify the table to set bins based on different breaks and record the frequency for each bin:

table http://dl.dropbox.com/u/43697/temporary/data_frame2.jpg

I guess the first thing to do wou开发者_JAVA百科ld be to set breaks on the year columns and later transpose them to match the AllotmentID.

Any thoughts?


Without a reproducible example, and no hint of what sort of graph you'd like to make using ggplot2, all I can do is point you in the direction of a useful tool. In this case I suspect you will want to make good use of the function cut, which is usually the go-to tool in R for binning.

cut will return a factor, which you will likely want to pass to table to count the number of observations in each "bin".

EDIT

Here's a very bare bones example. You will want to tinker with it to make the breaks and bins work out the way you want them to:

set.seed(123)
X <- runif(100,30,60)
table(cut(X,breaks=seq(30,60,by=5),labels=paste(as.character(seq(30,55,by=5)),"%",sep="")))
0

精彩评论

暂无评论...
验证码 换一张
取 消