Guest User

Untitled

a guest
Feb 24th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. # pollutantmean()
  2. pollutantmean <- function(directory, pollutant, id = 1:332) {
  3. csv_files <- list.files(directory) # 使用內建函數 list.files() 建立出 CSV 檔案路徑
  4. csv_file_paths <- paste0(directory, csv_files)
  5. csv_file_paths <- csv_file_paths[id] # 依照輸入的 id 參數選擇性讀入
  6. df_list <- list()
  7. pollutant_vector <- vector()
  8. for (i in 1:length(csv_file_paths)) {
  9. df_list[[i]] <- read.csv(csv_file_paths[i])
  10. pollutant_vector <- c(pollutant_vector, df_list[[i]][, pollutant]) # 將讀入測站的污染物資料合併起來
  11. }
  12. pollutant_mean <- mean(pollutant_vector, na.rm = TRUE) # 記得將 na.rm 參數設為 TRUE
  13. return(pollutant_mean)
  14. }
Add Comment
Please, Sign In to add comment