首页 > 解决方案 > R编程 - 顺序函数无法与包含一位和两位数字变量的列正确工作

问题描述

因此,我尝试使用 order 函数为我提供一个按最佳函数中所需结果变量排序的数据框。如果结果列中只有两位数,则 order 和 best 函数似乎都可以正常工作。

该列应从 8.1 到 15.8 排序,但结果最终是从 10.0 到 15.8 和 8.1 到 10.0

排序函数后的数值列 订单函数2后的数字列

best <- function(state, outcome){
  setwd('C:/users/nikumar/datasciencecoursera')
  ocm <- read.csv('outcome-of-care-measures.csv', colClasses = 'character', na.strings = "Not Available", stringsAsFactors = FALSE)
  index <- numeric()
  if ((state %in% ocm$State) == FALSE) {
    stop("invalid state")
  }
  if (outcome == 'heart attack') {
    index <- 11
  }
  else if (outcome == 'heart failure' ) {
    index <- 17
  }
  else if (outcome == 'pneumonia') {
    index <- 23
  }
  else{
    stop("invalid outcome")
  }
  ocm1 <- ocm[ocm$State == state,]
  ocm1 <- ocm1[,c(2,7,index)]
  ocm1 <- ocm1[order(ocm1[3],ocm1[1], decreasing = FALSE),]
  ocm1 <- na.omit(ocm1)
  View(ocm1)

  ocm1[1,1]

}

标签: r

解决方案


推荐阅读