2016-04-14 96 views
1

我在我的代码中收到一条错误消息,无法弄清楚。 我谷歌的一些问题,但仍然混淆解决方案。 如果你能检查我的代码并帮我解决这个问题,我将非常感激。 非常感谢。 我的代码是:<>中的错误:'closure'类型的对象不是子集表格仍然不知道如何修复它

rm(list = ls()) 
    library(XLConnect) 
    setwd('C:/Users/YL1/Desktop/Air Qulaity/Power Plant/ 
      NOx_SO2_Emission') # replace it with your own directory 
    file <- 'C:/Users/YL1/Desktop/Air Qulaity/ 
     Power Plant/NOx_SO2_Emission/ 
     Total Emission_2003-2015.xlsx' 
    wb <- loadWorkbook(file) 
    dt <- lapply(2003:2015, function(x) readWorksheet(wb, sheet = as.character(x))) 
    dt <- do.call(rbind, dt) 
    colnames(dt) <- c('State', 'Facility.Name', 'Facility.ID.ORISPL', 'Year', 
         'SO2.tons', 'NOx.tons') 

    dt.select.fun <- function(station) { 
     dt.select <- dt[dt$Facility.Name == station, ] 
     dt.select <- dt.select[order(-dt.select$Year), ] 
     write.csv(dt.select, paste0(station, '.csv')) 
     return(dt.select) 
    } 

    # change station to other values to extract the emission in other stations 
    dt.select.fun(station = 'Coffeen') 


> Error in dt$Facility.Name : object of type 'closure' is not 
> subsettable 

回答

0

那很可能是因为您的dt <- do.call(rbind, dt)导致矩阵。而且,你正在索引一个函数。将其替换为

dt <- do.call(rbind, dt) %>% as.data.table # to make a datatable 

当您调用您的函数时,您在哪里传递数据表?

更新

dt.select.fun <- function(data,y) { 
     dt.select <- data[data$Facility.Name == y, ] 
     dt.select <- dt.select[order(-dt.select$Year), ] 
     write.csv(dt.select, paste0(station, '.csv')) 
     return(dt.select) 
    } 
# call the function on your data table 
dt.select.fun(dt,"Coffeen") 
+0

感谢。但问题仍然存在。 –

+0

感谢您的更新!它似乎工作,但我得到新的错误:dt.select.fun(dt,station =“898”)中的错误:未使用的参数(dt)。如果您想获得更多详细信息,我可以发送我的代码和数据以供您查看。请留下电子邮件。非常感谢。 –

+0

您需要将参数传递给函数dt.select.fun < - function(dt,station){...您的代码... return(dt.select)}' – user5249203

相关问题