4

I am tring to sort c alphabetically if x[i]== x[i+1]. I used order() function but it changes the x column as well. I want to order the entire row:

 best <- function(state){
 HospitalName<-vector()
 StateName<-vector()
 HeartAttack<-vector()

  k<-1

  outcome<-read.csv("outcome-of-care-measures.csv",colClasses= "character")

 temp<-(outcome[,c(2,7,11,17,23)])

for (i in 1:nrow(temp)){
 if(identical(state,temp[i,2])==TRUE){
    HospitalName[k]<-temp[i,1]
    StateName[k]<-temp[i,2]
    HeartAttack[k]<-as.numeric(temp[i,4])
    k<-k+1
     }}
    frame<-data.frame(cbind(HospitalName,StateName,HeartAttack))


  library(dplyr)
  frame %>%
  group_by(as.numeric(as.character(frame[,3]))) %>%
  arrange(frame[,1])
    }

  Output:
                               HospitalName StateName HeartAttack
 1              FORT DUNCAN MEDICAL CENTER        TX         8.1
 2         TOMBALL REGIONAL MEDICAL CENTER        TX         8.5
 3        CYPRESS FAIRBANKS MEDICAL CENTER        TX         8.7
 4                  DETAR HOSPITAL NAVARRO        TX         8.7
 5                  METHODIST HOSPITAL,THE        TX         8.8
 6         MISSION REGIONAL MEDICAL CENTER        TX         8.8
 7  BAYLOR ALL SAINTS MEDICAL CENTER AT FW        TX         8.9
 8       SCOTT & WHITE HOSPITAL-ROUND ROCK        TX         8.9
 9         THE HEART HOSPITAL BAYLOR PLANO        TX           9
 10    UT SOUTHWESTERN UNIVERSITY HOSPITAL        TX           9
..                                    ...       ...         ...
Variables not shown: as.numeric(as.character(frame[, 3])) (dbl)

Output does not contain the HeartAttack Column and I do not understand why?

15
  • simply can do as follow x[order(x$c), , drop = FALSE] x is the name of your data and c is the column you want to rank etc Commented Feb 25, 2015 at 10:21
  • @Nemo This will only order column c. It is not what the OP wants. Commented Feb 25, 2015 at 10:28
  • @LyzandeR I see, however, it is appeared to be a duplicated question :-) Commented Feb 25, 2015 at 10:32
  • This is not a duplicate question. The OP wants to order column c only if column x has sequentially the same number. I voted to reopen this. Commented Feb 25, 2015 at 12:21
  • @LyzandeR OP wants to order the data first by "x", then by "c". In the link I posted, the data is also ordered by two variables (by "z" (descending) and "b"). I fail to see the fundamental difference here. Among the different answers, there is also a dplyr alternative. Commented Feb 25, 2015 at 12:39

1 Answer 1

5

One solution with dplyr:

library(dplyr)
df %>%
  group_by(x) %>%
  arrange(c)

Or as @Akrun mentions in the comments below just

df %>%
  arrange(x,c)

if you are not interested in grouping. Depends on what you want.

Output:

Source: local data frame [5 x 2]
Groups: x

  x c
1 2 A
2 2 D
3 3 B
4 3 C
5 5 E

There is another solution in base R but it will only work if your x column is ordered as is, or if you don't mind changing the order it has:

> df[order(df$x, df$c), , drop = FALSE]
  x c
2 2 A
1 2 D
4 3 B
3 3 C
5 5 E
Sign up to request clarification or add additional context in comments.

11 Comments

But R cannot find dplyr
Yeah, you need to install it first with install.packages('dplyr') and then load it with library(dplyr) for it to work.
That needs a contributed package, is not base R.
@GeorgeDontas And why can't someone use an external package? The question doesn't mention base R anywhere. This is not a reason for a downvote... And the OP does not want a simple ordering of a column.
@GeorgeDontas The OP wants to order the column c according to column x i.e. group ordering. I dont see why I shouldnt use dplyr. Can you explain please?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.