How to use function input to order a column in my function [R] -


i have spent lot of time on getting syntax down without success, put away revisiting class of object not data.frame, read in using read_csv{readr} (and hunch important.

 class(tt) [1] "tbl_df"     "tbl"        "data.frame" 

below original post:


its easier show mean. note: can if statement work, want sort column heart attack.

test <- function(state,output) {         if  ((state %in% unique(tt$state)) &               (output %in% names(tt)[3:5])){                  tt2 <- subset(tt, tt$state==state)                  #tt3 <- tt2[order(tt2$output),]                 #tt3$`hospital name`[1]                   print(head(tt2))         }          else print("yenoo") } 

and output:

> test("al","heart attack") # tibble: 6 × 5                    `hospital name` state `heart attack` `heart failure` pneumonia                              <chr> <chr>          <dbl>           <dbl>     <dbl> 1 southeast alabama medical center    al           14.3            11.4      10.9 2    marshall medical center south    al           18.5            15.2      13.9 3   eliza coffee memorial hospital    al           18.1            11.3      13.4 4         mizell memorial hospital    al             na            13.6      14.9 5      crenshaw community hospital    al             na            13.8      15.8 6    marshall medical center north    al             na            12.5       8.7 

now struggling give if condition expression (head(tt2) test see if if condition working) sort , give hospital name the row lowest heart attack.

i can outside function this:

> tt2 <- subset(tt, tt$state=="tx") > tt3 <- tt2[order(tt2$`heart attack`),] > tt3$`hospital name`[1] [1] "cypress fairbanks medical center" 

i been going @ moment , might not make sense. need break. in advance.

yeap mentioned while editing using {readr} function read_csv returned object not data.frame

class(tt) [1] "tbl_df"     "tbl"        "data.frame"  

now there more , need fix kinks now: introducing this

tt<-as.data.frame(tt) 

combined

tt3 <- tt2[order(tt2[output]),] 

allows me something, illustrated following call:

test("tx","heart failure") [1] "fort duncan medical center"  

my function , code not (included completeness)

# function reads outcome-of-care-measures.csv file , returns character vector # name of hospital has best (i.e. lowest) 30-day mortality specified outcome # in state.  tt <- read_csv("outcome-of-care-measures.csv", col_types = cols_only(`hospital 30-day death (mortality) rates heart attack`= col_double(),`hospital 30-day death (mortality) rates heart failure`= col_double(),`hospital 30-day death (mortality) rates pneumonia`= col_double(),`state`=col_character(),`hospital name`=col_character()))   names(tt)[3:5] <- c("heart attack","heart failure", "pneumonia")   tt<-as.data.frame(tt)  # first test if in input valid test <- function(state,output) {         if  ((state %in% unique(tt$state)) &               (output %in% names(tt)[3:5])){                  tt2 <- subset(tt, tt$state==state)                  tt3 <- tt2[order(tt2[output]),]                  tt3$`hospital name`[1]           }  else print("input error; use 2 letter state abbrevation, , heart attack, heart failure, or pneumonia outcome")  } 

Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -