[R] read.xls -> rotate data.frame

Kenn Konstabel lebatsnok at gmail.com
Fri Mar 25 18:01:20 CET 2011


Hi Knut,

On Fri, Mar 25, 2011 at 10:43 AM, Knut Krueger <rh at knut-krueger.de> wrote:
> Hi to all,
> how could I  to rotate automatically a data sheet which was imported by
> read.xls?
>
>     x1 x2 x3 .... xn
> y1 1   4  7   ...  xn/y1
> y2 2   5  8   .... xn/y2
> y3 3   6  9    ....xn/y2
> yn ... ... ...         Xn/Yn
>
>
> to
>
>      y1 y2  y3   ....  yn
> x1  1  2    3     ..... Yn/x1
> x2  4  5    6     ....  Yn/x2
> x3  7  8   9      ....  Yn/x2
> xn  ...   ...  ...   .....  Yn/xn

But think first why exactly do you need it? If for printing and/or
presentation rather than further analysis, then you can you can
transform your data frame to a "character" matrix and use use it this
way. t(df) would do it, or t(sapply(df, as.character)) might be safer
if you have factors in there.

If you want to use the transformed data frame in an analysis, I can
see two sensible possibilities:

1. Just modify the analysis so that instead of x[i,j] it would use
x[j,i] -- in many cases this would be the best solution
2. Extract the numeric (or whatever other type) columns from your data
frame, transform it to matrix with as.matrix and then "rotate".
t(as.matrix(df[,sapply(df, is.numeric)])) or something alike

Regards,

Kenn



More information about the R-help mailing list