Hi:
It would help if you named your variables such that alphanumeric ordering
doesn't disturb your variable ordering. Having been burned by this a few
times, I've learned the basics of sprintf() :) Here's your example
revisited, along with an alternative stacking/unstacking display with
package reshape.
library(reshape)
vnames <- paste('X', sprintf('%02d', c(1, 2, 10)), sep =
'')> vnames
[1] "X01" "X02" "X10"
testdata <- data.frame(1:5, 1:5, 1:5)> names(testdata) <- vnames
> testdata
X01 X02 X10
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5> melt(testdata) # analogous to stack()
Using as id variables
variable value
1 X01 1
2 X01 2
3 X01 3
4 X01 4
5 X01 5
6 X02 1
7 X02 2
8 X02 3
9 X02 4
10 X02 5
11 X10 1
12 X10 2
13 X10 3
14 X10 4
15 X10 5
cast(melt(testdata), value ~ variable) # analogous to unstack()
Using as id variables
value X01 X02 X10
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
HTH,
Dennis
On Sat, Mar 12, 2011 at 1:30 AM, Matthew Carroll <mjc510@york.ac.uk>
wrote:
> Dear R users,
>
> I'm having some problems with the stack() and unstack() functions, and
> wondered if you could help.
>
>
>
> I have a large data frame (400 rows x 2000 columns), which I need to reduce
> to a single column of values (and therefore 800000 rows), so that I can use
> it in other operations (e.g., generating predictions from a GLM object).
> However, the problem I'm having can be reproduced with something as
simple
> as this:
>
>
>
> > testdat <- data.frame("1" = c(1:5), "2" =
c(1:5), "10" = c(1:5))
>
> > testdat
>
> X1 X2 X10
>
> 1 1 1 1
>
> 2 2 2 2
>
> 3 3 3 3
>
> 4 4 4 4
>
> 5 5 5 5
>
>
>
> Please note that the numeric column names are what I'm dealing with in
my
> real data frame too. Stacking into a long format works fine:
>
>
>
> > stacked <- stack(testdat)
>
> > stacked
>
> values ind
>
> 1 1 X1
>
> 2 2 X1
>
> 3 3 X1
>
> 4 4 X1
>
> 5 5 X1
>
> 6 1 X2
>
> 7 2 X2
>
> 8 3 X2
>
> 9 4 X2
>
> 10 5 X2
>
> 11 1 X10
>
> 12 2 X10
>
> 13 3 X10
>
> 14 4 X10
>
> 15 5 X10
>
>
>
> But the problem I have comes in unstacking, when the order of the columns
> is
> changed from the original, to something alphabetical (i.e. X10 goes before
> X2):
>
>
>
> > unstacked <- unstack(stacked)
>
> > unstacked
>
> X1 X10 X2
>
> 1 1 1 1
>
> 2 2 2 2
>
> 3 3 3 3
>
> 4 4 4 4
>
> 5 5 5 5
>
>
>
> Maintaining the order of columns is very important for me, as I'm
dealing
> with spatial data.
>
> Is there a way of modifying the 'form' argument in unstack to
maintain
> numerical ordering of columns? Or, is there a quick way of re-ordering the
> columns in a data frame into a numerical order? Alternatively, is there
> another function or package that could do this? I've tried reshape()
but it
> seems very slow when I use my full data frame with it.
>
>
>
> Any help would be very much appreciated,
>
> Many thanks,
>
> Matthew Carroll
>
>
>
> --
>
> Matthew Carroll
>
> Department of Biology (Area 18),
>
> University of York,
>
> E-mail: mjc510@york.ac.uk
>
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@r-project.org 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.
>
[[alternative HTML version deleted]]