[R] creating categorical frequency tables from continuous data

Sascha Vieweg saschaview at gmail.com
Thu Jan 27 16:31:52 CET 2011


On 11-01-27 14:58, Louise Mair wrote:

> Hello,
>
> I am working with a dataset which essentially has only one column - a list of 
> distances in metres, accurate to several decimal places. eg
>
> distance
> 1000
> 6403.124
> 1000
> 1414.214
> 1414.214
> 1000
>
> I want to organise this into a frequency table, grouping into categories of 0 
> - 999,  1000 - 1999, 2000-2999 etc. I'd also like the rows where there are no 
> data points in that category to contain 0, in order to be able to plot a 
> histrogram with a linear x axis, and to statistically analyse differences 
> between datasets.
>
> I have tried table()  which doesn't group the data the way I'd like it, I've 
> also tried cut() but couldn't make it work. Ideally I'd like the output to 
> look something like this...
>
> distance        frequency
> 0-999            0
> 1000-1999   3
> 2000-2999   0
> ...

Could be a starting point for testing:

x <- abs(rnorm(500, 5000, 3000)
br <- seq(0, 20000, 1000)
summary(cut(x, br, labels=br[-1], include.lowest=T, 
ordered_result=T))

Look at ?cut to find out more. Good luck, *S*

>
> Any suggestions that are an improvement on doing it manually please?
>
> Thanks in advance!
>
> Louise
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>

-- 
Sascha Vieweg, saschaview at gmail.com



More information about the R-help mailing list