【问题标题】:How to use function input to order a column in my function [R]如何使用函数输入对我的函数中的列进行排序 [R]
【发布时间】:2016-11-17 20:03:36
【问题描述】:

我花了很多时间来降低语法但没有成功,现在我要重新访问的是我的对象的那个类 不只是 data.frame,我使用 read_csv{readr} 读取它(我的直觉是这可能很重要。

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

以下是原帖:


更容易表达我的意思。注意:我可以让 if 语句工作,现在我想对心脏病发作的列进行排序。

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")
}

还有一些输出:

> test("AL","Heart Attack")
# A 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

现在我正在努力为 if 条件提供一个表达式(head(tt2) 只是一个测试以查看 if 条件是否有效),它将对Heart Attack 最低的行进行排序并给出医院名称。

我可以在函数之外这样做:

> tt2 <- subset(tt, tt$State=="TX")
> tt3 <- tt2[order(tt2$`Heart Attack`),]
> tt3$`Hospital Name`[1]
[1] "CYPRESS FAIRBANKS MEDICAL CENTER"

我一直在讨论这个问题,可能没有意义。需要休息一下。 提前致谢。

【问题讨论】:

  • 试试这个方法:order(tt2[output])。如果在调用函数时输出作为“心脏病发作”传递
  • 可能是R中的前5个错误。运算符$只是为了方便,而不是函数式编程。
  • 谢谢。这是我脑海中浮现的一件事,现在我确定了。并就替代方案审查哪些主题提供建议。

标签: r


【解决方案1】:

是的,正如我在使用 {readr} 函数 read_csv 编辑时提到的,返回的对象不是 data.frame,而是

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

现在还有更多内容,我需要修复所有问题,但现在: 介绍一下

tt<-as.data.frame(tt)

结合

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

允许我做某事,如以下调用所示:

test("TX","Heart Failure")
[1] "FORT DUNCAN MEDICAL CENTER" 

我的功能和代码不是很好,但现在可以(为了完整性而包含在内)

# The function reads the outcome-of-care-measures.csv file and returns a character vector
# with the name of the hospital that has the best (i.e. lowest) 30-day mortality for the specified outcome
# in that state.

tt <- read_csv("outcome-of-care-measures.csv", col_types = cols_only(`Hospital 30-Day Death (Mortality) Rates from Heart Attack`= col_double(),`Hospital 30-Day Death (Mortality) Rates from Heart Failure`= col_double(),`Hospital 30-Day Death (Mortality) Rates from 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 is 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, and Heart Attack, Heart Failure, or Pneumonia as outcome")

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-18
    • 2019-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-04
    • 2012-11-30
    相关资源
    最近更新 更多