[R] Converting data.frame from long to wide format

Matthew Pettis
Thu, 09 Dec 2010 02:26:16 -0000

Hi,

I was wondering if there is an easy way that I am missing for turning a long
dataframe into a wide one.  Below is sample code that will make what I have
and, in comments, the form of what I want:

    #   Have: dataframe like 'df'
df <- expand.grid( x=LETTERS[1:3], y=LETTERS[4:6])
df$z <- letters[1:length(df[,1])]

# Want: data.frame that has following form:
#       A   B   C
#   D   a   b   c
#   E   d   e   f
#   F   g   h   i


I looked at 'xtabs' and 'cast' from reshape/reshape2, but unless
I'm
misunderstanding something, these will work only for the 'z' column
being
numeric, not textual.  Is there an easy way to do this with 'z' being
textual rather than numeric?

tia,
Matt

	[[alternative HTML version deleted]]