[R] Rearranging long dataframe for printing in Sweave (and recoding a factor)
Jean-Louis Abitbol
abitbol at sent.com
Sun Jan 24 13:24:11 CET 2010
Dear R-Helpers,
I have to print a dataframe with >1000 rows and 2 columns for a report
done with Sweave.
I could use Hmisc latex function with longtable option.
However it is a waiste of space and paper given that I have only 2 (or
sometime 3) columns in the dataframe.
So it came to my mind that I could maybe rearrange the dataframe and
create several new variables from the original 2 in a tabular way for
printing in less space.
I started with this:
library(Hmisc)
test <- data.frame(conc=trunc(runif(1000, 1,
1000)),nam=rep(c("A","B"),500))
test$groups <- cut2(test$conc,g=10)
The idea being to use cut2 to get 10 groups with approx the same number
of rows and then subset on the groups to create 10 new variables.
I am stuck right away as cut2 produces test$groups with the levels:
levels(test$groups)
[1] "[ 1,100)" "[100,215)" "[215,312)" "[312,411)" "[411,500)"
"[500,598)" "[598,700)" "[700,797)" "[797,890)" "[890,997]"
and so my first question is: how do I recode the levels in a more or
less automatic way (ie not having to spell them) so that they are
replaced by a factor with values 1 to 10 in this case or more generally
in 1 to g levels ? (and then I can subset in a loop and create conc1,
conc2, conc3 etc..)
My second question is: is there a function around or an options in one
of the latex functions to do what I have explained at the beginning of
this mail ! I did not find that in Hmisc latex or in xtable.
Thanks to all for your help.
Best wishes, JL
--
Jean-Louis Abitbol
abitbol at sent.com
Tel 33 (0)6 24 47 59 34
1 ter Rue Céline, 92160
Antony, France
More information about the R-help
mailing list