Question

I have a table with in game user purchases, the columns are:

uid levelid created timeid price

I want by using the reshape's cast function extract for each unique player the minimum values of "timeid"s (timeid - is the time of purchase in unixformat) plus I want the minimum values of "levelid"s (levelid - is the players level on which the purchase has been made) and the last thing - I want to add the "created" value to the table (created - is the time when the user installed the game)

To do the cast with one value is quite simple, I use such a code

pivot<-cast(sbw0214, uid + created ~ ., min, value='timeid')

but what should I write if I want to add the levelid?

The

pivot<-cast(sbw0214, uid + created ~ ., min, value=c('levelid', 'timeid'))

is not working.

UPD: as asked below, added some dummy data

         uid levelid    created     timeid  price
1    859053      12 1392080130 1392703139   6.99
2   5878556      16 1392092198 1392305961  66.00
4   5878556      18 1392092198 1392541437 649.00
8   5878556      14 1392092198 1392092388 229.00
12  6580096      11 1391707958 1392149148  66.00
13  6582371      10 1392400831 1392584819   2.49
14  6582371      12 1392400831 1392752459   4.99
15  6993255      14 1392318992 1392569031  13.99
16  6993255       9 1392318992 1392391040   1.49
22  7437087       8 1392734333 1392832027   1.99
23  7437087       8 1392734333 1392859236   3.99
24  7437087       8 1392734333 1392832005   1.99
25  7484797      17 1391578980 1391626414  19.99
Was it helpful?

Solution

You can use aggregate for this task:

aggregate(cbind(timeid, levelid) ~  uid + created, sbw0214, min)

      uid    created     timeid levelid
1 7484797 1391578980 1391626414      17
2 6580096 1391707958 1392149148      11
3  859053 1392080130 1392703139      12
4 5878556 1392092198 1392092388      14
5 6993255 1392318992 1392391040       9
6 6582371 1392400831 1392584819      10
7 7437087 1392734333 1392832005       8

Update based on comment:

If you want to apply different functions, the package plyr is very helpful.

library(plyr)
ddply(sbw0214, .(uid, created), summarise,
      timeid = min(timeid),
      price = sum(price))

      uid    created     timeid  price
1  859053 1392080130 1392703139   6.99
2 5878556 1392092198 1392092388 944.00
3 6580096 1391707958 1392149148  66.00
4 6582371 1392400831 1392584819   7.48
5 6993255 1392318992 1392391040  15.48
6 7437087 1392734333 1392832005   7.97
7 7484797 1391578980 1391626414  19.99
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top