[R] reshape without timevar argument?

Tiago R Magalhaes tiago17 at socrates.Berkeley.EDU
Sat Feb 26 07:06:38 CET 2005


Hi

I have a data.frame with 2 columns. The first column is an ID column. 
The other columns are description of the ids. There is more than one 
description for each Id.
Want I want to get as a value is a data.frame where each row 
corresponds to one ID and has as many columns as different 
descriptions.

I have used a very convoluted step, but I'm very convinced there is 
an easier way to do this.

Basically, I used the reshape function, but even in this convulated 
way there is a step that I can't solve. I used a "fake" timevar using 
the table function.

df <- data.frame(id=c(rep('IDa',3), rep('IDb', 5), rep('IDc', 2),
                    rep('IDd',5)), let=letters[1:5])
#add Freq to each id
xFreqdf <- merge(df, table(df['id']), by.x='id', by.y='Var1')
xFreq <- xFreqdf[,'Freq']
#general way of transforming xFreq into:
xFreq2 <- c(3:1, 5:1, 2:1, 5:1)

#substitute Freq by xFreq2
xFreqdf['Freq'] <- xFreq2

#get final df
xReshape <- reshape(xFreqdf, idvar='id', v.names='let',
                     timevar='Freq', direction='wide')

The final data.frame doesn't order the columns in the way I would 
think the more obvious: let.1 is actually the 4th column instead of 
the second one.

I again thank the kindness of people on this list




More information about the R-help mailing list