Re: [R] convert decimals to fractions - sorted

From: Muhammad Subianto <msubianto_at_gmail.com>
Date: Wed 26 Jul 2006 - 21:35:42 EST

Dear all,
Thanks for your help.
I played with you suggest and still didn't sort (summary) which I need.

> t(table(at2[sort.order,c(1,3)]))

       V1
jeebee -1 1
  0 0 4
  11/21 0 1
  1/2 1 0
  1/21 1 1
  13/42 1 1
  17/42 0 2

  2/21   0 3
  3/14   1 2
  5/42   0 1
  8/21   0 1

>

I need the result summary (order) like,

      -1 1

  0/42   0 4
  2/42   1 1
  4/42   0 3
  5/42   0 1
  9/42   1 2
  13/42  1 1
  16/42  0 1
  17/42  0 2

  21/42 1 0
  22/42 0 1

Thanks very much for any suggestions.
Groeten & Regards, Muhammad Subianto

On 7/26/06, JeeBee <JeeBee@troefpunt.nl> wrote:
>
> Hi Muhammad,
>
> How about this?
>
> at <- read.table(textConnection(a))
> at2 <- cbind(at, jeebee=as.character(as.fractions(as.numeric(at[,2]))))
>
> sort.order <- order(at2$V2)
>
> at2[sort.order,]
> at2[sort.order,c(1,3)]
>
> JeeBee.
>
> ______________________________________________
> R-help@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.
>

On 7/25/06, Muhammad Subianto <msubianto@gmail.com> wrote:
> Dear all,
> Based on my question a few months ago
> https://stat.ethz.ch/pipermail/r-help/2006-January/086952.html
> and solved with
> https://stat.ethz.ch/pipermail/r-help/2006-January/086955.html
> https://stat.ethz.ch/pipermail/r-help/2006-January/086956.html
> and from
> https://stat.ethz.ch/pipermail/r-help/2006-January/086958.html
>
> frac.fun <- function(x, den){
> dec <- seq(0, den) / den
> nams <- paste(seq(0, den), den, sep = "/")
> sapply(x, function(y) nams[which.min(abs(y - dec))])
> }
> #######################
> frac.fun(c(0, 1, 0.8266667, .066666, 0.2666666), 75)
>
> Now, I have a dataset something like this:
>
> a <-"1 0
> 1 0.095238095238095
> 1 0.214285714285714
> -1 0.5
> 1 0.309523809523810
> -1 0.0476190476190476
> 1 0.404761904761905
> 1 0.119047619047619
> -1 0.214285714285714
> -1 0.309523809523810
> 1 0
> 1 0
> 1 0.404761904761905
> 1 0.095238095238095
> 1 0.047619047619047
> 1 0.380952380952381
> 1 0.214285714285714
> 1 0.523809523809524
> 1 0
> 1 0.095238095238095"
>
> First, I make it as fractions and then sorted.
> I have played around to make it sort, but it didn't succes.
>
> df <- read.table(textConnection(a))
> library(MASS)
> as.fractions(as.numeric(df[,2]))
> cbind(table(df[,2], df[,1]), summary(as.factor(df[,2])))
> table(frac.fun(as.numeric(df[,2]),42), df[,1])
> > table(frac.fun(as.numeric(df[,2]),42), df[,1])
>
> -1 1
> 0/42 0 4
> 13/42 1 1
> 16/42 0 1
> 17/42 0 2
> 21/42 1 0
> 22/42 0 1
> 2/42 1 1
> 4/42 0 3
> 5/42 0 1
> 9/42 1 2
> >
>
> How to make the result as sort (to increase) like this,
>
> -1 1
> 0/42 0 4
> 2/42 1 1
> 4/42 0 3
> 5/42 0 1
> 9/42 1 2
> 13/42 1 1
> 16/42 0 1
> 17/42 0 2
> 21/42 1 0
> 22/42 0 1
>
> Thank's for any help.
>
> Best, Muhammad Subianto
>



R-help@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. Received on Wed Jul 26 21:51:46 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 28 Jul 2006 - 22:17:02 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.