0

# I've been attempting to get this to work for days. No luck. Each #vector is 3947 in length, and I've changed between as.numeric, #as.data.frame, with inputs. Any suggestions would be welcome.

#pulling data from outcomes on hospitals, with eventual attempt to return ranking based on this.
rankall<-function(outcome,num) {
  library(datasets)
  library(plyr)
  library(dplyr)
  data<-read.csv("outcome-of-care-measures.csv", na.strings="Not Available",
                 stringsAsFactors=FALSE)

  if (outcome == "pneumonia"){
    column_index<-23
  } else if (outcome == "heart attack") {
    column_index<-11
  } else if (outcome == "heart failure") {
    column_index<-17
  } else {
    stop("invalid outcome")
  } 

  data2<-cbind(data[,2],data[,7],data[,column_index])
  data2<-na.omit(data2)
  colnames(data2)<-c("hospital", "state", outcome)
  order1<-data2[order(as.numeric(data2[,outcome]), data2[,"hospital"]),]
  numrows<-nrow(order1)
  as.data.frame(order1) %>%
    arrange(outcome, .by_group=TRUE)

  print(class(data2[,"hospital"]))
  print(class(data2[,2]))
  print(class(data2[,3]))
}  
Matt
  • 7,255
  • 2
  • 12
  • 34
Nathan
  • 1

1 Answers1

1

You need to make outcome a symbol, it is now seen as a string. See this also. To rectify, do:

library(dplyr)

data2 = data.frame(x=runif(100),
hospital=sample(LETTERS,100,replace=TRUE),
"heart attack" = rbinom(100,1,0.5),
"pneumonia" = rbinom(100,1,0.5),check.names=FALSE)

outcome = "pneumonia"

data2 %>% arrange(outcome, .by_group=TRUE)
Error: incorrect size (1) at position 1, expecting : 100

data2 %>% arrange(!!sym(outcome), .by_group=TRUE)

              x hospital heart attack pneumonia
1   0.471584775        B            0         0
2   0.907479862        S            1         0
3   0.141569308        Q            0         0
4   0.511258807        A            0         0
StupidWolf
  • 45,075
  • 17
  • 40
  • 72