r - Issue with sorting one column after rank is assigned -


*****this deal question asked in coursera , hence may not able reveal complete code*****

hi, below data frame (outcome_h)

hospital_name   h_a h_f pn abc             4.5  5  6 cde             4.5  1  3 efg             5    2  1 

1) need rank column provided in function call (it 1 of h_a ,h_f,pn)

2) there rank provided in call. need match rank rank calculated above , return respective hospital_name

i had used ties.method="first" solve tie problem. when @ final output hospital name not sorted. example: if give rank =2, expect cde printed, due problems(which note aware) abc gets printed rank=2 , cde printed rank=1.

below parts of code better understanding:

h_a <- as.numeric(outcome_h$h_a) ha <- h_a[order(h_a)]       // newly added piece order value df <- data.frame(ha,round(rank(ha,ties.method="first")),outcome_h$hospital_name) rowss <- df[order(df$round.rank.ha..),] 

before ordering output:

     ha round.rank.ha..          outcome_h.hospital.name 42  8.1               1       fort duncan medical center 192 8.5               2  tomball regional medical center 61  8.7               4           detar hospital navarro 210 8.7               4 cypress fairbanks medical center 69  8.8               6  mission regional medical center 117 8.8               6           methodist hospital,the 

after ordering output:

  ha round.rank.ha..ties.method....first...                       outcome_h.hospital.name 1 8.1                                      1                  providence memorial hospital 2 8.5                                      2      memorial hermann baptist orange hospital 3 8.7                                      3              peterson regional medical center 4 8.7                                      4 children's hospital -scott & white healthcare 5 8.8                                      5            united regional health care system 6 8.8                                      6              st joseph regional health center 

as can see, data hospital names incorrect.

any appreciated.

thanks, pravellika j

you try h_a <- as.numeric(as.character(outcome_h$h_a))

output

ha round.rank.ha..ties.method....first... outcome_h.hospital_name 1 4.5                                      1                     abc 2 4.5                                      2                     cde 3 5.0                                      3                     efg 

Comments

Popular posts from this blog

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -