[R] Help with Hmisc, cut2, split and quantile

Peter Ehlers ehlers at ucalgary.ca
Mon Mar 8 19:19:03 CET 2010


On 2010-03-08 8:47, Guy Green wrote:
>
> Hello,
> I have a set of data with two columns: "Target" and "Actual".  A
> http://n4.nabble.com/file/n1584647/Sample_table.txt Sample_table.txt  is
> attached but the data looks like this:
>
> Actual	Target
> -0.125	0.016124906
> 0.135		0.120799865
> ...		...
> ...		...
>
> I want to be able to break the data into tables based on quantiles in the
> "Target" column.  I can see (using cut2, and also quantile) how to get the
> barrier points between the different quantiles, and I can see how I would
> achieve this if I was just looking to split up a vector.  However I am
> trying to break up the whole table based on those quantiles, not just the
> vector.
>
> The following code shows me the ranges for the deciles of the "Target" data:
> library(Hmisc)
> read_data=read.table("C:/Sample table.txt", head = T)
> table(cut2(Read_data$Target,g=10))
>
> However I would like to be able to break the table into ten separate tables,
> each with both "Actual" and "Target" data, based on the "Target" data
> deciles:
>
> top_decile = ...(top decile of "read_data", based on Target data)
> next_decile = ...and so on...
> bottom_decile = ...

I would just add a factor variable indicating to which decile
a particular observation belongs:

  dat$DEC <- with(dat, cut(Target, breaks=10, labels=1:10))

If you really want to have separate data frames you can then
split on the decile:

  L <- split(dat, dat$DEC)


    -Peter Ehlers

>
> That way I could manipulate the deciles, graph them separately (and
> together) and so on, just as easily as I can the whole table.  I'm sure this
> must be simple, but I can't see the way forward.  I have also looked at
> split() and quantile() but have not been able to get them to achieve what I
> am after.  Can anybody see a simple way foward on this?
>
> Thanks,
> Guy

-- 
Peter Ehlers
University of Calgary



More information about the R-help mailing list