How to change the order of rows?

51 views Asked by At

Please advise how to control rows order in flextable.

For example, in the table inside The Epidemiologist R Handbook the order of rows is arranged in the ascending way, and Other and Missing rows in the middle of the table. But I want to have these rows in the end. How to do this?

https://epirhandbook.com/en/tables-for-presentation.html

[enter image description here](https://i.stack.imgur.com/9vt9o.png)

I expect that rows order in the table will looks like this:

Central Hospital
Military Hospital
Port Hospital
St. Mark's Maternity Hospital (SMMH)
Other
Missing
Total
1

There are 1 answers

0
Manderlog On

The easiest way is to use the index of the rows you need:

ids=which(df$Hospital %in% c('Other', 'Missing', 'Total') #which rows should be at the bottom of a dataframe

df2=df[ids,] #  create temporaty dataframe with this rows only;

df=df[-c(ids),] #remove them from your dataframe

df=rbind(df,df2) # append this rows to the bottom.

#Or,probably, easier:

ids=which(df$Hospital %in% c('Other', 'Missing', 'Total') 

df=rbind(df[-c(ids),],df[ids,])