在R中使用split函数获取此错误

时间:2017-01-29 19:24:51

标签: r split subset

我是R的新手并通过Coursera学习,在this pdf中的一个作业大纲中,我遇到了以下错误:

Error in split.default(x = seq_len(nrow(x)), f = f, drop = drop, ...) :   

group length is 0 but data length > 0

基本上我被困在第2部分找到状态部分的最佳医院,并在为其编写功能时,我通过控制台测试其构造。到目前为止测试它我写了这段代码:

outcome <- read.csv("outcome-of-care-measures.csv", colClasses = "character")
outcome[, 11] <- as.numeric(outcome[, 11]) # 30-day mortality for the heart attack
outcome[, 17] <- as.numeric(outcome[, 17]) # 30-day mortality for the heart failure 
outcome[, 23] <- as.numeric(outcome[, 23]) # 30-day mortality for the pneumonia 
outcome <- outcome[, c(2, 7, 11)] # Subsetting three columns
outcome <- na.omit(outcome) # omitting na
names(outcome) <- c("hospital", "state", "outcome") #Renaming the columns
outcome <- split(outcome, outcome$State) # Splitting it based on states

我还附上Hospital Sample

希望任何人都能指出我正确的方向。

1 个答案:

答案 0 :(得分:1)

应该是state,而不是State。看看数据:

> head(outcome)
                          hospital state outcome
1 SOUTHEAST ALABAMA MEDICAL CENTER    AL    14.3
2    MARSHALL MEDICAL CENTER SOUTH    AL    18.5
3   ELIZA COFFEE MEMORIAL HOSPITAL    AL    18.1

您将其命名为state,然后尝试拆分名为State的列。 R区分大小写。