R语言处理PM2.5文件

setwd('C:/Users/LHW/Desktop/Hubei task120210918/')
site_loc <- read.table("clipboard",sep="\t",header = T)
avector = c()
for(atmp in site_loc['监测点编码']) {
    
     avector <- atmp }

library(readxl)
library(lubridate)

#Author: Tianshu Chen
#Mail: [email protected]

#path for folder
data_path="C:/Users/LHW/Desktop/Hubei task120210918/original_data_2019_01_04_2020_01_04"

#path for results
output_path="C:/Users/LHW/Desktop/Hubei task120210918/extract_pm25/1290A.csv"

#site id
site_id=avector

#type
type_list=c("PM2.5") #type: PM2.5, PM10, SO2, NO2, O3, O3_8h, CO, AQI, PM2.5_24h, PM10_24h, SO2_24h, NO2_24h, O3_24h, O3_8h_24h, CO_24h. 

#___________code__________#
#get wd and file
split_path <- function(x) if (dirname(x)==x) x else c(basename(x),split_path(dirname(x)))
wd_path<-paste0(paste0(rev(gsub("/", "", split_path(data_path)[-1])), collapse="/"),"/")    
setwd(wd_path)                                                               
file_name = list.files(basename(data_path))

#list for data                                                      
dir = paste("./", basename(data_path), "/", file_name, sep="")                                       
n = length(dir)

#creat empty df
data_df <- data.frame(matrix(vector(),ncol=3+length(site_id)))
colnames(data_df) <-c("date","hour","type",site_id)

#add data to datadf
for (i in 1:n){
    
    
  if(any(grepl("\\d",site_id))){
    
      
    daliy_data <- read.csv(file = dir[i],header=T,na.strings = "NA")
  }else{
    
    
    daliy_data <- read.csv(file = dir[i],header=T,na.strings = "NA",encoding="UTF-8")      
  }
  if("date" %in% colnames(daliy_data)){
    
    
    #remove "X" from names of daliy_data
    colnames(daliy_data)=sub("X", "", colnames(daliy_data), fixed = TRUE)           
    #fliter by site and type
    tar_daliy_data <- daliy_data[,c("date","hour","type",site_id)]
    if(!is.null(type_list)){
    
    
      tar_daliy_data=tar_daliy_data[which(tar_daliy_data$"type"%in%type_list),]
    }           
    data_df <- rbind(data_df,tar_daliy_data)
    print(dir[i])
  }
}

#tidy date
data_df$date<-as.Date(as.character(data_df$date),format="%Y%m%d")
data_df$date <- data_df$date + hours(data_df$hour)
data_df=data_df[,!names(data_df)=="hour"]

#output
write.csv(data_df,output_path,row.names = FALSE)
#___________code__________#

参考文献:
https://zhuanlan.zhihu.com/p/377372556?utm_source=wechat_session&utm_medium=social&utm_oi=34703805513728&s_r=0

猜你喜欢

转载自blog.csdn.net/weixin_45577825/article/details/120374758
今日推荐