[R] convert decimals to fractions - sorted

JeeBee JeeBee at troefpunt.nl
Fri Jul 28 13:42:22 CEST 2006


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



More information about the R-help mailing list