0

After I did a cast function with the original data table I've got the matrix which looks like this (just a sample of it)

ctime   01/13   01/14   02/13   02/14
01/13   10003   2057    11830   1061
01/14   0       3722    0       4088
02/13   0       1485    4765    1267
02/14   0       0       0       3810
03/13   0       5268    0       4829

this is the amount of users transactions in the month with column's name, who came in the month of ctime column. Like for example, users who came in January-13 paid 10003 times in the January-13, but, after a year in January-14 the same users (who hasn't left the game) paid 2057 times.

The matrix now is in data frame format with characters inside.

I want to have matrix like this

ctime   01/13   02/13   01/14   02/14
01/13   100038  115830  20573   18061
02/13   0       47065   14385   12637
03/13   0       0       54268   48029
01/14   0       0       37282   42088
02/14   0       0       0       38910

Can you please give me a tip, how can I order the matrix by ctime column and by rows please?

andrew-zmeul
  • 121
  • 1
  • 1
  • 10
  • to sort the column by names, just refer to http://stackoverflow.com/questions/7334644/r-rearrange-column-names-of-dataframe-alphabetically-or-order-user-wants – hrbrmstr Mar 13 '14 at 18:49
  • there is quite a different task with no answer to my question, I don't know if it's appropriate to intrude the discussion with mine But thank you – andrew-zmeul Mar 16 '14 at 12:35

1 Answers1

0

To use strptime to load your date (which will enable ordering with order) you'll also need to also provide the day of the month:

# Load data
dat <- read.table(text="ctime   01/13   01/14   02/13   02/14
01/13   10003   2057    11830   1061
01/14   0       3722    0       4088
02/13   0       1485    4765    1267
02/14   0       0       0       3810
03/13   0       5268    0       4829", header=T)
names(dat) <- c("ctime", "01/13", "01/14", "02/13", "02/14")

# Use strptime to order rows and columns
dat2 <- dat[order(strptime(paste("01/", dat$ctime), "%d/%m/%y")),-1]
dat2 <- dat2[,order(strptime(paste("01/", names(dat2)), "%d/%m/%y"))]
dat2 <- cbind(ctime=dat$ctime, dat2)
dat2
#   ctime 01/13 02/13 01/14 02/14
# 1 01/13 10003 11830  2057  1061
# 3 01/14     0  4765  1485  1267
# 5 02/13     0     0  5268  4829
# 2 02/14     0     0  3722  4088
# 4 03/13     0     0     0  3810
josliber
  • 43,891
  • 12
  • 98
  • 133
  • With your sample it is working but not on my entire data frame. No errors while executing, the table stays in the same condition it was before. Plus, it seems it simply do not add "01/" to anything Maybe I lost something in my code, I use such lines 'pivotpay2<-pivotpay[order(strptime(paste("01/", pivotpay$ctime),"%d/%m/%y")),-1] pivotpay2<-pivotpay2[,order(strptime(paste("01/", names(pivotpay2)), "%d/%m/%y"))]' – andrew-zmeul Mar 16 '14 at 15:02
  • found the solution `dat2 <- dat[order(strptime(paste("01/", dat$ctime), "%d/%m/%Y")),] dat2 <- dat2[,order(strptime(paste("01/", names(dat2)), "%d/%m/%Y"))] rownames(dat2)<-dat2$ctime dat2 <- dat2[,-ncol(dat2)] dat2` – andrew-zmeul Mar 19 '14 at 16:10