[R] reshape direction=wide

Vaclav Petricek vaclav.petricek at mff.cuni.cz
Thu Feb 19 15:45:50 CET 2004


Hello

I am reshaping a data.frame bids --> reshaped as shown below.

I thought this should be possible with a single invocation of
reshape, but the only way I came up with is reshaping subsets for each
keyword and then joining them together. Does anyone have an idea how to
solve this in a more elegant way? Efficiency is a concern as the datasets
are very large.

Is there a way to specify multiple v.names?

bids
        batch     keyword rank        id  bid
1   312221627   Broadband    1 401173481 2.64
2   312221627   Broadband    2 236096320 2.63
3   312221627   Broadband    3 367411639 2.62
4   312221627   Broadband    4 188906982 2.61
5   312221627   Broadband    5 227691359 2.01
205 312221627 Outsourcing    1 406300683 3.68
206 312221627 Outsourcing    2  12862485 3.65
207 312221627 Outsourcing    3 237944232 3.65
208 312221627 Outsourcing    4  95867634 3.64

reshaped
       batch   keyword bid.1 bid.2 bid.3 bid.4 bid.5      id.1      id.2      id.3      id.4      id.5
1  312221608 Broadband  2.63  2.62  2.62  2.61  2.01 236096320 401173481 367411639 188906982 227691359
2  312221617 Broadband  2.64  2.63  2.62  2.61  2.01 401173481 236096320 367411639 188906982 227691359
3  312221627 Broadband  2.64  2.63  2.62  2.61  2.01 401173481 236096320 367411639 188906982 227691359
4  312221639 Broadband  2.65  2.64  2.63  2.62  2.01 188906982 401173481 236096320 367411639 227691359
5  312221649 Broadband  2.65  2.64  2.63  2.62  2.01 188906982 401173481 236096320 367411639 227691359
6  312221659 Broadband  2.65  2.64  2.63  2.62  2.01 188906982 401173481 236096320 367411639 227691359
7  312221708 Broadband  2.65  2.64  2.63  2.62  2.01 188906982 401173481 236096320 367411639 227691359
8  312221719 Broadband  2.65  2.64  2.63  2.62  2.01 188906982 401173481 236096320 367411639 227691359
9  312221729 Broadband  2.65  2.64  2.63  2.62  2.01 188906982 401173481 236096320 367411639 227691359
10 312221739 Broadband  2.65  2.64  2.63  2.62  2.01 188906982 401173481 236096320 367411639 227691359


keywords <- levels(bids[,'keyword'])

reshaped <<- NULL
for (key in keywords)
{
	keywordbid <- bids[bids[,'keyword']==key,]
	keywordbid[,'id'] = NULL
	keywordbid <- reshape(keywordbid, v.names='bid',
	                      timevar='rank',direction='wide',idvar='batch')
	keywordid <- bids[bids[,'keyword']==key,]
	keywordid[,'bid'] = NULL
	keywordid <- reshape(keywordid, v.names='id',
	                     timevar='rank',direction='wide',idvar='batch')
	if(is.null(reshaped))
	{
		reshaped <<- merge(keywordbid, keywordid)
	} else {
		reshaped <<- rbind(reshaped, merge(keywordbid, keywordid))
	}
	rm(keywordbid, keywordid)
}

Thank you very much for any comments,

--

Vaclav Petricek
http://kocour.ms.mff.cuni.cz/~petricek




More information about the R-help mailing list