1

I get a error message on my code and can not figure it out . I google some questions but still confuse about the solution. I will be very appreciate if you can check my code and help me to solve this issue. Thanks a lot. My code is:

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
Lee Yee
  • 17
  • 1
  • 8

1 Answers1

0

That is probably because your dt <- do.call(rbind, dt) results in a matrix. Moreover, you are indexing in a function. Replace that with

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

When you are calling your function , where are you passing data table ?

update:

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")
user5249203
  • 4,436
  • 1
  • 19
  • 45
  • Thanks. but the problem is still there. – Lee Yee Apr 14 '16 at 16:19
  • Thanks for update ! It seems work but I get new error:Error in dt.select.fun(dt, station = "898") : unused argument (dt). If you like to get more details I can send my code and data for your review. Please leave an email. Thanks a lot. – Lee Yee Apr 14 '16 at 17:03
  • you need to pass the argument to your function `dt.select.fun <- function(dt,station) {...your code... return(dt.select) }` – user5249203 Apr 14 '16 at 17:28
  • Hi I change the code as below but still get the error message:`Error in dt$Facility.Name : object of type 'closure' is not subsettable` – Lee Yee Apr 14 '16 at 17:37
  • Hi, Thanks a lot. But the problem is still there. Please check the code I posted below. – Lee Yee Apr 14 '16 at 20:03
  • Check the updated version. The error is because you are passing `df`as parameter in function and using `dt` in the function code. dt is a null hence, it is not subsettable . The update should work – user5249203 Apr 14 '16 at 20:12
  • Hello, Thank you so much for your help. It totally works ! I appreciate your help very much !~ – Lee Yee Apr 14 '16 at 20:41
  • I have rate your answer but it not show up. But the your update made my day ! Best Regards – Lee Yee Apr 14 '16 at 20:43
  • Hi, I just accept your answer. But I can not up-vote it due to my reputation. Thanks for your help ! – Lee Yee Apr 14 '16 at 21:00
  • No problem. Thank you. Appreciate it. – user5249203 Apr 14 '16 at 21:01
  • Thank you very much for all your help ! – Lee Yee Apr 14 '16 at 21:14
  • Hi @user5249203, Thank you very much for your help. I have another R question and would you help me to check it ? Thanks !http://stackoverflow.com/questions/36635894/how-to-calculate-the-annual-average-from-time-series-data – Lee Yee Apr 15 '16 at 06:33