[R] How to have original (name) order after melt and cast command

Rui Barradas ruipbarradas at sapo.pt
Wed Jul 18 13:18:54 CEST 2012


Hello,

Try the following.

# This is your code
df_sorted = df[order(as.Date(df$date, "%m/%d/%Y"), decreasing = TRUE),]

# This is my code
nams <- as.character(unique(dat1$name))
nums <- sapply(nams, function(nm) which(names(df_sorted) %in% nm))
df_sorted[, sort(nums)] <- df_sorted[, nams]
names(df_sorted)[sort(nums)] <- nams
df_sorted


Hope this helps,

Rui Barradas

Em 18-07-2012 11:52, Vincy Pyne escreveu:
> Dear R helpers,
>
> I have a data.frame as given below -
>
> dat1 = data.frame(date = as.Date(c("3/30/12","3/29/12","3/28/12","3/27/12","3/26/12",
> "3/23/12","3/22/12","3/21/12","3/20/12", "3/30/12","3/29/12","3/28/12","3/27/12",
> "3/26/12","3/23/12","3/22/12","3/21/12","3/20/12", "3/30/12","3/29/12","3/28/12",
> "3/27/12","3/26/12","3/23/12","3/22/12","3/21/12","3/20/12"), format="%m/%d/%y"),
>
> name = as.character(c("xyz","xyz","xyz","xyz","xyz","xyz","xyz","xyz", "xyz","abc", "abc","abc","abc","abc","abc", "abc","abc","abc","lmn","lmn", "lmn","lmn",  "lmn","lmn", "lmn","lmn","lmn")),
>
> rate = c(c(0.065550707, 0.001825007, 0.054441969, 0.020810572, 0.073430586, 0.037299722, 0.099807733, 0.042072817, 0.099487289, 5.550737022, 4.877620777,  5.462477493, 4.972518082, 5.01495407, 5.820459609, 5.403881954, 5.009506516,
> 4.807763909, 10.11885434,10.1856975,10.04976806,10.15428632, 10.20399335, 10.22966704,10.20967742,10.22927793,10.02439192)))
>
>> dat1
>           date      name         rate
> 1  2012-03-30  xyz  0.065550707
> 2  2012-03-29  xyz  0.001825007
> 3  2012-03-28  xyz  0.054441969
> 4  2012-03-27  xyz  0.020810572
> 5  2012-03-26  xyz  0.073430586
> 6  2012-03-23  xyz  0.037299722
> 7  2012-03-22  xyz  0.099807733
> 8  2012-03-21  xyz  0.042072817
> 9  2012-03-20  xyz  0.099487289
> 10 2012-03-30  abc  5.550737022
> 11 2012-03-29  abc  4.877620777
> 12 2012-03-28  abc  5.462477493
> 13 2012-03-27  abc  4.972518082
> 14 2012-03-26  abc  5.014954070
> 15 2012-03-23  abc  5.820459609
> 16 2012-03-22  abc  5.403881954
> 17 2012-03-21  abc  5.009506516
> 18 2012-03-20  abc  4.807763909
> 19 2012-03-30  lmn 10.118854340
> 20 2012-03-29  lmn 10.185697500
> 21 2012-03-28  lmn 10.049768060
> 22 2012-03-27  lmn 10.154286320
> 23 2012-03-26  lmn 10.203993350
> 24 2012-03-23  lmn 10.229667040
> 25 2012-03-22  lmn 10.209677420
> 26 2012-03-21  lmn 10.229277930
> 27 2012-03-20  lmn 10.024391920
>
>
> attach(dat1)
>
> library(plyr)
> library(reshape)
>
>
> in.melt <- melt(dat1, measure = 'rate')
> (df = cast(in.melt, date ~ name))
>
> df_sorted = df[order(as.Date(df$date, "%m/%d/%Y"), decreasing = TRUE),]
>
>
>> df_sorted
>          date         abc         lmn         xyz
> 9 2012-03-30    5.550737 10.11885 0.065550707
> 8 2012-03-29    4.877621 10.18570 0.001825007
> 7 2012-03-28    5.462477 10.04977 0.054441969
> 6 2012-03-27    4.972518 10.15429 0.020810572
> 5 2012-03-26    5.014954 10.20399 0.073430586
> 4 2012-03-23    5.820460 10.22967 0.037299722
> 3 2012-03-22    5.403882 10.20968 0.099807733
> 2 2012-03-21    5.009507 10.22928 0.042072817
> 1 2012-03-20    4.807764 10.02439 0.099487289
>
>
> My Problem :-
>
> The original data.frame has the order name as "xyz", "abc" and "lmn". However, after melt and cast command, the order in the "df_sorted" has changed to "abc", "lmn" and " xyz". How do I maintain the original order in "df_sorted" i.e. I need
>
>          date       xyz                 abc           lmn
>
> 9 2012-03-30   0.065550707   5.550737   10.11885
>
> 8 2012-03-29   0.001825007   4.877621   10.18570
>
> 7 2012-03-28   0.054441969   5.462477   10.04977
>
> 6 2012-03-27   0.020810572   4.972518   10.15429
>
> 5 2012-03-26   0.073430586   5.014954   10.20399
>
> 4 2012-03-23   0.037299722   5.820460   10.22967
>
> 3 2012-03-22   0.099807733   5.403882   10.20968
>
> 2 2012-03-21   0.042072817   5.009507   10.22928
>
> 1 2012-03-20   0.099487289   4.807764   10.02439
>
>
> Kindly guide
>
> Thanking in advance
>
> Vincy
>
>
> 	[[alternative HTML version deleted]]
>
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list