[R] How to swap and rearrange rows?
Gabor Grothendieck
ggrothendieck at gmail.com
Fri May 16 13:50:47 CEST 2008
This can be shortened slightly using the builtin month.abb in place of m:
ind <- match(substr(rownames(est), 1, 3), month.abb)
On Fri, May 16, 2008 at 5:38 AM, Abhijit Dasgupta
<adasgupt at mail.jci.tju.edu> wrote:
> Another possibility:
> # Generate the months in sequential order:
> m <- months(seq(as.Date('2000/1/1'), by='month',length=12),abbreviate=T)
> # Concatenate the data
> est=rbind(est31,est30,est28)
> # generate the right order
> ind <- match(substr(rownames(est),1,3),m)
> # reorder the rows
> est <- est[ind,]
>
> BTW, Richard meant "rbind" instead of "cbind" in his code, which I'm sure you knew.
>
> Abhijit
>
> On Fri, 16 May 2008 10:09:22 +0100
> Richard.Cotton at hsl.gov.uk wrote:
>
>> > How to swap and rearrange the row so that I will have
>> > Jan-Dec in order?
>> > > est31
>> > p0 est.alpha est.beta est.rate
>> > Jan 0.8802867 0.7321440 7.241757 0.1380880
>> > Mar 0.8598566 0.7096567 7.376367 0.1355681
>> > May 0.6204301 0.8657272 6.036106 0.1656697
>> > July 0.5032258 0.9928488 4.027408 0.2482986
>> > Aug 0.5322581 0.9625738 4.103121 0.2437169
>> > Oct 0.6792115 0.8526226 5.105218 0.1958780
>> > Dec 0.8397849 0.7490287 7.070349 0.1414357
>> > > est30
>> > p0 est.alpha est.beta est.rate
>> > Apr 0.7296296 0.7929348 6.303877 0.1586325
>> > Jun 0.5574074 0.8588608 5.695905 0.1755647
>> > Sept 0.6066667 0.9031150 4.594891 0.2176330
>> > Nov 0.7725926 0.7600906 5.636366 0.1774193
>> > > est28
>> > p0 est.alpha est.beta est.rate
>> > Feb 0.877262 0.6567584 8.708051 0.1148363
>> > Thank you so much.
>>
>> First, concatenate the data frames:
>> est <- cbind(est31, est30, est28)
>>
>> Now you can sort the resulting data frame using order, as described in FAQ
>> on R 7.23.
>> months <- factor(rownames(est), levels=c("Jan", "Feb", "Mar", "Apr",
>> "May", "Jun", "July", "Aug", "Sept", "Oct", "Nov", "Dec"))
>> sortedest <- est[order(months),]
>>
>> (You might also want to recode 'July' to 'Jul' and 'Sept' to 'Sep' to be
>> consistent with the other months.)
>>
>> Regards,
>> Richie.
>>
>> Mathematical Sciences Unit
>> HSL
>>
>>
>> ------------------------------------------------------------------------
>> ATTENTION:
>>
>> This message contains privileged and confidential info...{{dropped:12}}
>
> ______________________________________________
> 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