I have a data frame with 2 columns. I have ordered them using order() function
data<-data[order(data$Mortality),]
head(data)
Hospital.Name Mortality
FORT DUNCAN MEDICAL CENTER 8.1
TOMBALL REGIONAL MEDICAL CENTER 8.5
DETAR HOSPITAL NAVARRO 8.7
CYPRESS FAIRBANKS MEDICAL CENTER 8.7
MISSION REGIONAL MEDICAL CENTER 8.8
METHODIST HOSPITAL,THE 8.8
3rd and 4th positions are ties (Mortality = 8.7 for both). I want to break the tie with alphabetical order in data$Hospital.Name so that "CYPRESS FAIRBANKS" is 3rd and "DETAR HOSPITAL" as 4th.
Use data$Hospital.Name as second argument in order:
R> data <- data[order(data$Mortality, data$Hospital.Name), ]
R> data
Hospital.Name Mortality
1 FORT DUNCAN MEDICAL CENTER 8.1
2 TOMBALL REGIONAL MEDICAL CENTER 8.5
4 CYPRESS FAIRBANKS MEDICAL CENTER 8.7
3 DETAR HOSPITAL NAVARRO 8.7
6 METHODIST HOSPITAL,THE 8.8
5 MISSION REGIONAL MEDICAL CENTER 8.8
Related
i have a data frame as below
Provider.Number Hospital.Name State Mortality
210001 MERITUS MEDICAL CENTER MD 12.5
210002 UNIVERSITY OF MARYLAND MEDICAL CENTER MD 12.7
210003 PRINCE GEORGES HOSPITAL CENTER MD 13
210004 HOLY CROSS HOSPITAL MD 9.6
210005 FREDERICK MEMORIAL HOSPITAL MD 9.8
210006 HARFORD MEMORIAL HOSPITAL MD 11.5
210007 SAINT JOSEPH MEDICAL CENTER MD 9.5
210008 MERCY MEDICAL CENTER INC MD 11.2
210009 JOHNS HOPKINS HOSPITAL, THE MD 10.2
210011 SAINT AGNES HOSPITAL MD 11.1
210012 SINAI HOSPITAL OF BALTIMORE MD 9.7
210013 BON SECOURS HOSPITAL MD 9.6
210015 MEDSTAR FRANKLIN SQUARE MEDICAL CENTER MD 9.3
210016 WASHINGTON ADVENTIST HOSPITAL MD 11
210017 GARRETT COUNTY MEMORIAL HOSPITAL MD 13.5
210018 MEDSTAR MONTGOMERY MEDICAL CENTER MD 9.3
210019 PENINSULA REGIONAL MEDICAL CENTER MD 10.6
210022 SUBURBAN HOSPITAL MD 9.9
210023 ANNE ARUNDEL MEDICAL CENTER MD 12
210024 MEDSTAR UNION MEMORIAL HOSPITAL MD 11.3
210027 WESTERN MARYLAND REGIONAL MEDICAL CENTER MD 12.6
210028 MEDSTAR SAINT MARY'S HOSPITAL MD 13.1
210029 JOHNS HOPKINS BAYVIEW MEDICAL CENTER MD 10.7
210030 CHESTER RIVER HOSPITAL CENTER MD 11.2
210032 UNION HOSPITAL OF CECIL COUNTY MD 9.9
210033 CARROLL HOSPITAL CENTER MD 9.7
210034 MEDSTAR HARBOR HOSPITAL MD 9.2
210035 CIVISTA MEDICAL CENTER MD 14.2
210037 MEMORIAL HOSPITAL AT EASTON MD 10.6
210038 MARYLAND GENERAL HOSPITAL MD 10.8
210039 CALVERT MEMORIAL HOSPITAL MD 10.1
210040 NORTHWEST HOSPITAL CENTER MD 12.6
210043 BALTIMORE WASHINGTON MEDICAL CENTER MD 12.7
210044 GREATER BALTIMORE MEDICAL CENTER MD 7.4
210045 EDWARD MCCREADY MEMORIAL HOSPITAL MD 12.9
210048 HOWARD COUNTY GENERAL HOSPITAL MD 10.1
210049 UPPER CHESAPEAKE MEDICAL CENTER MD 12.9
210051 DOCTORS' COMMUNITY HOSPITAL MD 11
210054 SOUTHERN MARYLAND HOSPITAL CENTER MD 11.7
210055 LAUREL REGIONAL MEDICAL CENTER MD 10.6
210056 MEDSTAR GOOD SAMARITAN HOSPITAL MD 8.4
210057 SHADY GROVE ADVENTIST HOSPITAL MD 11.7
210060 FORT WASHINGTON HOSPITAL MD 11
210061 ATLANTIC GENERAL HOSPITAL MD 10.8
21020F VA MARYLAND HEALTHCARE SYSTEM - BALTIMORE MD 12.6
i want to sort the DF based on mortality column and then alphabetical order of Hospital column to handle ties.
I tired using different sort functions like
sorted <- work[order(work$Mortality,work$Hosptial),]
or
with(work, order(Mortality, Hospital))
but the final sorted outcome is wrong.
The output i got is
Hosptial State Mortality
CALVERT MEMORIAL HOSPITAL MD 10.1
HOWARD COUNTY GENERAL HOSPITAL MD 10.1
JOHNS HOPKINS HOSPITAL, THE MD 10.2
LAUREL REGIONAL MEDICAL CENTER MD 10.6
MEMORIAL HOSPITAL AT EASTON MD 10.6
PENINSULA REGIONAL MEDICAL CENTER MD 10.6
JOHNS HOPKINS BAYVIEW MEDICAL CENTER MD 10.7
ATLANTIC GENERAL HOSPITAL MD 10.8
MARYLAND GENERAL HOSPITAL MD 10.8
DOCTORS' COMMUNITY HOSPITAL MD 11
FORT WASHINGTON HOSPITAL MD 11
WASHINGTON ADVENTIST HOSPITAL MD 11
SAINT AGNES HOSPITAL MD 11.1
CHESTER RIVER HOSPITAL CENTER MD 11.2
MERCY MEDICAL CENTER INC MD 11.2
MEDSTAR UNION MEMORIAL HOSPITAL MD 11.3
HARFORD MEMORIAL HOSPITAL MD 11.5
SHADY GROVE ADVENTIST HOSPITAL MD 11.7
SOUTHERN MARYLAND HOSPITAL CENTER MD 11.7
ANNE ARUNDEL MEDICAL CENTER MD 12
MERITUS MEDICAL CENTER MD 12.5
NORTHWEST HOSPITAL CENTER MD 12.6
VA MARYLAND HEALTHCARE SYSTEM - BALTIMORE MD 12.6
WESTERN MARYLAND REGIONAL MEDICAL CENTER MD 12.6
BALTIMORE WASHINGTON MEDICAL CENTER MD 12.7
UNIVERSITY OF MARYLAND MEDICAL CENTER MD 12.7
EDWARD MCCREADY MEMORIAL HOSPITAL MD 12.9
UPPER CHESAPEAKE MEDICAL CENTER MD 12.9
PRINCE GEORGES HOSPITAL CENTER MD 13
MEDSTAR SAINT MARY'S HOSPITAL MD 13.1
GARRETT COUNTY MEMORIAL HOSPITAL MD 13.5
CIVISTA MEDICAL CENTER MD 14.2
GREATER BALTIMORE MEDICAL CENTER MD 7.4
MEDSTAR GOOD SAMARITAN HOSPITAL MD 8.4
MEDSTAR HARBOR HOSPITAL MD 9.2
MEDSTAR FRANKLIN SQUARE MEDICAL CENTER MD 9.3
MEDSTAR MONTGOMERY MEDICAL CENTER MD 9.3
SAINT JOSEPH MEDICAL CENTER MD 9.5
BON SECOURS HOSPITAL MD 9.6
HOLY CROSS HOSPITAL MD 9.6
CARROLL HOSPITAL CENTER MD 9.7
SINAI HOSPITAL OF BALTIMORE MD 9.7
FREDERICK MEMORIAL HOSPITAL MD 9.8
SUBURBAN HOSPITAL MD 9.9
UNION HOSPITAL OF CECIL COUNTY MD 9.9
Function that gets 2 arguments:
state(check the original CSV data)
heart attack/ heat failure/ Pneumonia
My complete code is
best <- function(states,outcomes)
{
#patterns is obtained to use them in the regex function
patterns<-paste("^Hospital.*",outcomes, sep="")
Readcsv<-read.csv("outcome-of-care-measures.csv", check.names = F)
columnname<-colnames(Readcsv)
#regex operation going on
regex1<-grep(patterns,columnname,ignore.case=TRUE, value = T)
#another regex operation
Extracted<-grep("Mortality",regex1,ignore.case=TRUE, value = T)
#extract dataframe based on the state and final extracted column name using the regex function
dfe<-subset(Readcsv, Readcsv$State == states & Readcsv[[Extracted]]!="Not Available")
#create a vector
b<-c("Hospital Name","State", Extracted)
#extract only those columns seen in the vector
work<-dfe[,b]
#change column name
colnames(work)<-c("Hosptial","State","Mortality")
# stuck after this point
Ascorder<-work[with(work, order(Mortality, Hosptial)),]
}
I am relatively new to Stack overflow, please mind my formatting issue. I would like to understand where I'm going wrong.
You can use dplyr for this:
require(dplyr)
work <- work %>%
arrange(Mortality, Hospital)
I couldn't test it because you didn't give a reproducible example of your data, but it should do the trick.
This question already has answers here:
Sort (order) data frame rows by multiple columns
(19 answers)
Closed 6 years ago.
HospitalName | Rating
-----------------------------------| ------
FORT DUNCAN MEDICAL CENTER | 8.1
TOMBALL REGIONAL MEDICAL CENTER | 8.5
DETAR HOSPITAL NAVARRO | 8.7
CYPRESS FAIRBANKS MEDICAL CENTER | 8.7
Here is my sample table , you can see In Hospital Name "DETAR HOSPITAL" and "Cypress FAIRBANKS " Having the same Rating . I have sorted the table by the least rating to highest rating but all I need is sort the Hospital Names alphabetically which have the same Rating, like "CYPRESS..." Should come first then "DETAR" though they having the same Rating but they need to be arranged alphabetically .
Can any one help me with this ?
We can use order
df1[order(df1$Rating, df1$HospitalName),]
# HospitalName Rating
#1 FORT DUNCAN MEDICAL CENTER 8.1
#2 TOMBALL REGIONAL MEDICAL CENTER 8.5
#4 CYPRESS FAIRBANKS MEDICAL CENTER 8.7
#3 DETAR HOSPITAL NAVARRO 8.7
If we are using dplyr, arrange is the way to go
library(dplyr)
df1 %>%
arrange(Rating, HospitalName)
row.names Hospital State Heart Attack Heart Failure
1 2275 PROVIDENCE MEMORIAL HOSPITAL TX 16.1 9.1
2 2276 MEMORIAL HERMANN BAPTIST ORANGE HOSPITALTX 16.3 14.3
4 2278 UNITED REGIONAL HEALTH CARE SYSTEM TX 17.4 15.1
5 2279 ST JOSEPH REGIONAL HEALTH CENTER TX 15.7 15.6
6 2280 PARKLAND HEALTH AND HOSPITAL SYSTEM TX 12.9 11.2
7 2281 UNIVERSITY OF TEXAS MEDICAL BRANCH GAL TX 17.4 11.8
Hello R peeps, I need to get a row.name where input, which is variable column name (Heart Attack or Heart Failure) is minimum for that column. In the exmple above, if I input "Heart failure" it needs to return [1] 2275Which row name in the first row. so far I got this:inds<-subset(wfperstate, wfperstate[[outname]]==min)where wfperstate is my data frame
outname is my inputPlease, help!
To transform my last comment into a function :
get_min_rowname <-
function(dat,col)
dat[which.min(dat[[col]]),"row.names"]
Then you apply it :
get_min_rowname(wfperstate, "Heart Attack")
get_min_rowname(wfperstate, "Heart Failure")
I have a dataframe:
df <- data.frame(c(name = "FORT DUNCAN", "DETAR HOSPITAL", "CYPRESS FAIRBANKS","MISSION REGIONAL", "Test"), rate = c(8.0,8.7,8.7,8.1,8.9))
colnames(df) = c("name","rate")
ordered_df <- df[order(df[,2]),]
name rate
1 FORT DUNCAN 8.0
4 MISSION REGIONAL 8.1
2 DETAR HOSPITAL 8.7
3 CYPRESS FAIRBANKS 8.7
5 Test 8.9
I can clearly order the dataframe by the rate variable. However, If two rates are similar then I want to order by name. i.e. Detar Hospital and Cypress Fairbanks have the same rate of 8.7. Therefore, I want Cypress Fairbanks to move up and Detar Hospital to move down and Test should remain at its place (The last place according to the rate)...
Any ideas???
Cheers
I think I fixed it by:
ordered_df <- df[order(df$rate, df$name),]
Cheers
Since order accepts many variables via ... you can do the following:
> df[order(df[,2],df[,1] ),]
name rate
1 FORT DUNCAN 8.0
4 MISSION REGIONAL 8.1
3 CYPRESS FAIRBANKS 8.7
2 DETAR HOSPITAL 8.7
5 Test 8.9
I have got the following list which was generated by using split function with state as index.
$AK
Hospital_Name State Mortality_Rate
99 PROVIDENCE ALASKA MEDICAL CENTER AK 13.4
100 MAT-SU REGIONAL MEDICAL CENTER AK 17.7
102 FAIRBANKS MEMORIAL HOSPITAL AK 15.5
$AL
Hospital_Name State Mortality_Rate
1 SOUTHEAST ALABAMA MEDICAL CENTER AL 14.3
2 MARSHALL MEDICAL CENTER SOUTH AL 18.5
3 ELIZA COFFEE MEMORIAL HOSPITAL AL 18.1
$AR
Hospital_Name State Mortality_Rate
193 SILOAM SPRINGS MEMORIAL HOSPITAL AR 15.6
194 JOHNSON REGIONAL MEDICAL CENTER AR 16.9
195 WASHINGTON REGIONAL MED CTR AT NORTH HILLS AR 15.2
I want to select Hospital Name of 2nd row from each of these states. Can somebody help with an apply function here? I was trying to use sapply the following way(not working) -
x <- sapply(test.case3,function(i) test.case3[[i]][2,1])
so that, by varying 'i', I can get result as follows -
> test.case3[[1]][2,1]
[1] "MAT-SU REGIONAL MEDICAL CENTER"
> test.case3[[2]][2,1]
[1] "MARSHALL MEDICAL CENTER SOUTH"
> test.case3[[3]][2,1]
[1] "JOHNSON REGIONAL MEDICAL CENTER"
Kindly advise.
I required to produce the final report in the following format.(ignore the data, in below example)
Hospital_Name State
D W MCMILLAN MEMORIAL HOSPITAL AL
ARKANSAS METHODIST MEDICAL CENTER AR
JOHN C LINCOLN DEER VALLEY HOSPITAL AZ