[R] convert decimals to fractions - sorted

Muhammad Subianto msubianto at gmail.com
Fri Jul 28 22:47:25 CEST 2006


Dear JeeBee and all,
It is nice. Thanks you very much.
I must learn much more about ?as.fractions, ?as.numeric, ?as.character
and ?table functions.

Best wishes, Muhammad Subianto


On 7/28/06, JeeBee <JeeBee at troefpunt.nl> wrote:
>
> Ah I see, I did not read your story well enough.
> You want to sort after applying table()
> Well, the idea I suggested was to keep the real numbers in,
> because the fractions are sorted as characters strings (alphabetically),
> which is not what you want. So, now I suggest the following:
>
> # First apply table()
> tmp1 <- as.data.frame(table(df))
>
> # Note that table() turned your numeric data into factors,
> # this might not be a handy approach, anyways, it is possible I guess.
> # You have to convert back using as.numeric(as.character(tmp1$V2))
> # or, more efficiently, as.numeric(levels(tmp1$V2))[tmp1$V2]
>
> # Add the column with the fractions
> tmp2 <- cbind(tmp1,
>         fracs=as.character(as.fractions(as.numeric(as.character(tmp1$V2)))))
>
> # Finally hide that sort colum if you want
>
> ( tmp2[-2] )
>
> # Everybody happy?
>
> JeeBee
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>



More information about the R-help mailing list