如何使用函数输入来命令我的函数中的列[R]

时间:2016-11-17 20:03:37

标签: r

我花了很多时间在语法上没有成功,现在我放弃了我正在重访的是我的对象类 不只是data.frame,我使用read_csv {readr}读取它(我的预感是重要的。

CREATE TRIGGER NumOfRecords
AFTER INSERT ON recording
FOR EACH ROW
BEGIN
UPDATE poem
SET NumRecordings = (SELECT COUNT(*) FROM recording WHERE poem.PoemTitle = recording.PoemTitle)

以下是原帖:

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

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

还有一些输出:

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

现在我正在努力给if条件一个表达式(head(tt2)只是一个测试,看看if条件是否正常),它将排序并给出最低{{1}行的医院名称}。

我可以在这样的函数之外做到这一点:

> 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

我一直在这一刻,可能没有意义。需要休息一下。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

正如我在使用{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")

}