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]]

