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?

double-beep
  • 5,031
  • 17
  • 33
  • 41
EnginO
  • 321
  • 3
  • 4
  • 8
  • 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 –  Feb 25 '15 at 10:21
  • @Nemo This will only order column c. It is not what the OP wants. – LyzandeR Feb 25 '15 at 10:28
  • @LyzandeR I see, however, it is appeared to be a duplicated question :-) –  Feb 25 '15 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. – LyzandeR Feb 25 '15 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. – Henrik Feb 25 '15 at 12:39
  • @Henrik Hi Henrik thanks for the comment. The difference would be in the case that his x column is not ordered. If the x column is something like `2,2,3,3,5,2,2` then according to the question and the OP would like to order column c once for the first `2,2` once for `3,3` and then another time for the next `2,2`, in which case he wouldn't want to alter the order of column x (he mentions he doesn't want column x to change and in which case my answer would not be correct). Anyway, maybe I should have waited first for the OP to clarify more. – LyzandeR Feb 25 '15 at 12:44
  • @LyzandeR Yes, absolutely!, I have already ordered my x column in increasing order and I want to order my entire rows alphabetically if I have same x values in different rows without affecting the increasing order I had done in column x – EnginO Feb 25 '15 at 12:53
  • @EnginO This is not very clear. If column x is ordered then this question was correctly identified as duplicate and the answers below are correct i.e. `df[order(df$x, df$c), , drop = FALSE]`. Can you be more specific? Why didn't the answers below help? – LyzandeR Feb 25 '15 at 12:59
  • @LyzandeR It is hard to explain the whole code. I can send you the entire code but I am not sure whether it is forbidden regarding to the rules of this forum – EnginO Feb 25 '15 at 13:10
  • @EnginO It is not forbidden to add code in your question. The exact opposite it is encouraged. But try to be precise. Use only the code / data that show your problem. If you include 300lines of code out of which 280 are irrelevant then this wont help. Try to show how your data looks like and why the answers below won't help. – LyzandeR Feb 25 '15 at 13:12
  • You probably need to show a better example of the data as well if the answers below wont help – LyzandeR Feb 25 '15 at 13:18
  • I added the entire code and I am learning R by attending R Programming Course in Coursera voluntarily and could not figure out how to handle alphabetical arrangement in the data frame. Since it is Course Assignment , I thought that I am not treated very nicely in the forum. – EnginO Feb 25 '15 at 13:30
  • Thanks a lot LyzandeR. I solve the problem with your edition arrange(x,c) – EnginO Feb 25 '15 at 13:57
  • You will get answers as long as you specify it is an assignment. Change `frame[,3]` and `frame[,1]` to the actual names i.e. `HeartAttack` and `HospitalName` and you should be fine. – LyzandeR Feb 25 '15 at 13:57
  • Oh that's great then :) – LyzandeR Feb 25 '15 at 13:58

1 Answers1

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
LyzandeR
  • 37,047
  • 12
  • 77
  • 87
  • But R cannot find dplyr – EnginO Feb 25 '15 at 10:18
  • Yeah, you need to install it first with `install.packages('dplyr')` and then load it with `library(dplyr)` for it to work. – LyzandeR Feb 25 '15 at 10:19
  • That needs a contributed package, is not base R. – gd047 Feb 25 '15 at 10:40
  • @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. – LyzandeR Feb 25 '15 at 10:42
  • @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? – LyzandeR Feb 25 '15 at 10:50
  • I also think that my question is not duplicate because I want to order characters alphabetically only if the the numbers in the data frame is equal. Still searching an answer for this – EnginO Feb 25 '15 at 12:08
  • Doesn't my second solution (the update) work? – LyzandeR Feb 25 '15 at 12:15
  • Also, is the x column sorted? Or can you have cases like `2,2,3,3,5,2,2` and how would you want those to be sorted? – LyzandeR Feb 25 '15 at 12:20
  • @LyzandeR I think the `group_by` can be avoided and use arrange with the variable group inside if the the OP is not doing any other statistics by group – akrun Feb 25 '15 at 12:24
  • @akrun Thanks Akrun. As always you re absolutely right. I am just wondering whether his x column is ordered or not. That's why I provided two solutions one grouped and one not. – LyzandeR Feb 25 '15 at 12:29
  • @akrun Thanks Akrun. I totally agree with you :) – LyzandeR Feb 25 '15 at 12:32