最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

sorting - Trying to group by a specified column, but returns "Error in UseMethod("group_by")&quot

programmeradmin4浏览0评论

I'm working in Rstudio and currently having an issue with the group_by() from dplyr package.

reduceddata <- complete.cases(specdata)
    group_by(State)
    arrange(validoutcomes, Hospital, .by_groups=TRUE) %>% 
    arrange(State) %>%
    mutate(rank = row_number()) %>%

Currently it returns the error: Error in UseMethod("group_by") : no applicable method for 'group_by' applied to an object of class "character"

The ultimate goal is to rank hospitals across all states. Instructions for the assignment here: .pdf

#confirm following packages are installed: "dplyr", "tidyverse","conflicted"
#and use conflict_prefer to specify package and command

rankall <- function(outcome, num = "best") {
  ## Read outcome data
  samedata <- read.csv("outcome-of-care-measures.csv", colClasses = "character")
  head(samedata)
  specdata <- samedata[,c(2,7,11,17,23)]
  names(specdata)<- c("Hospital", "State", "Heart attack", "Heart failure", "Pneumonia")
  print(names(specdata))
  ## Check that state and outcome are valid
  
  validoutcomes <- c("Heart attack", "Heart failure", "Pneumonia")
  if (!any(outcome %in% validoutcomes)) {
    stop("invalid outcome")}
  
  if(!any(State == specdata$State)) {
    stop("invalid state")}

    reduceddata <- complete.cases(specdata)
    group_by(State)
    arrange(validoutcomes, Hospital, .by_groups=TRUE) %>% 
    arrange(State) %>%
    mutate(rank = row_number()) %>%
    
    ifelse (num == "best") {
      specdata %>%
        filter(rank == min(rank)) %>%
        select(Hospital, State)
    }
    if (num == worst) {
      specdata %>%
        group_by(State) %>%
        filter(rank == max(rank)) %>%
        select(Hospital, State)
    }
    else if {
      specdata %>%
        group_by(State)
      filter(rank == num) %>%
        select(Hospital, State)
    }
  
 
    }
## For each state, find the hospital of the given rank
  ## Return a data frame with the hospital names and the
  ## (abbreviated) state name

#Try it:
#rankall("Heart failure", best)

I've tried indexing it to group_by(specdata$State) with the same error. At the start of reading the code, I specified colClasses = "character". I tried eliminating this and running the group_by(State) line then gave the error of State column not found along with errors in a few other lines, so I put it back in. I get the same error code if I try to run the function as-is in its entirety. Error in group_by(): ! Must group by variables found in .data. ✖ Column state is not found.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论