环境科学研究员,谁是新编程,对我轻松!
我有两个空气质量仪器(沙尘和ptrak)记录数据,并将它们存储为.csv文件。我的目标是通过函数式编程实现数据清理过程的自动化。每台仪器以不同的时间间隔记录(30秒和1秒),每个仪器都有一个独特的标头。
我已经有了一个读取ptrak数据的函数。它删除讨厌的标头,并将日期和时间列转换为一个as.POSIX日期时间。结果是一种新的宽格式数据,只有两列,日期时间和粒子数浓度(pnc)。
下面是ptrak函数:
## assume there is only one file per directory for now
read.ptrak<-function(fpath){
x<-read.csv(fpath,skip=30,header=FALSE,stringsAsFactors=FALSE) #removing the first 30 rows of garbage
colnames(x) <- c("date","time","pnc") #creating my own header
##merge date and time column together
x$datetime<-strptime(paste(x$date,x$time), "%m/%d/%Y %H:%M:%S", tz="UTC")
## convert the first column to a posix timestamp
x$datetime<-as.POSIXct(x$datetime,format=dt_format, tz="UTC")
x<-x[,-c(1:2)] ## remove redundant variables date, and time
x<-x[,c(2:1)] ## reorder columns so datetime is first
return(x)
}
#okay now we can apply our function to our ptrak csv file:
ptrak_data <- read.ptrak(**INSERT FILE PATH HERE**)
head(ptrak_data)
#everything looks great!我遇到麻烦的地方是尘封的数据。而不是为每个观察的日期和时间列,我只提供了一个开始时间,位于标题内。实际的dataframe只提供从这个开始时间到30秒间隔的总运行时间。我想要创建一个新的dataframe,它有一个POSIX时间戳和五个粒子质量浓度(见下文),以后我可以通过datetime与ptrak合并。有人能提供一个函数来使用开始时间和经过的时间来创建一个新的日期时间向量,然后移除标头,这样我就有了两列的宽格式的dataframe吗?
这里是我第一次尝试清理尘封数据:
read.dtrak<-function(fpath){
x<-read.csv(fpath,skip=36,header=FALSE,stringsAsFactors=FALSE)
colnames(x)<-c("elapsedtime","pm1","pm2.5","pm4","pm10","total","alarms","errors")
## need to read in the same file again and keep the header to extract the start time and start date:
y<-read.csv(fpath,skip=6,header=FALSE,stringsAsFactors=FALSE)
colnames(y)<-c("variable","value") ## somewhat arbitrary colnames for temporary df
starttime <-y[1,2]
startdate <-y[2,2]
startdatetime <- strptime(paste(startdate,starttime), "%m/%d/%Y %H:%M:%S", tz="UTC")
#convert to posix timestamp:
startdatetime <-as.POSIXct(startdatetime, format=dt_format, tz="UTC")
## create a new variable called datetime in dataframe 'x'
x$datetime <- startdatetime + x$elapsedtime ## this is giving me the following error: "Error in unclass(e1) + unclass(e2) : non-numeric argument to binary operator
return(x)
}最终目标是生成一个与ptrak数据类似的清理数据,除了报告一个粒子数浓度(pnc)之外,还需要有PM1、PM2.5、PM4、PM10和dusttrak_data.csv(参见dusttrak_data.csv)。
为未将样本数据包括在文章中,事先表示歉意。我不知道如何创建包含这些烦人的标题的示例数据!
找到这个问题的答案基本上可以节省我100小时的手工数据清理工作,所以我非常感谢你的洞察力!
以下是数据:普瑞克,沙得瑞克 编辑:Dave2e解决方案的转换为感兴趣者的函数。
read.dtrak<-function(fpath){
sdate<-read.csv(fpath, header=FALSE, nrow=1, skip =7)
stime <-read.csv(fpath, header = FALSE, nrow=1, skip=8)
startDate<-as.POSIXct(paste(sdate$V2, stime$V2), "%m/%d/%Y %H:%M:%S", tz="UTC")
x<-read.csv(fpath, skip=36, stringsAsFactors = FALSE)
names(x)<-c("elapsedtime","pm1","pm2.5","pm4","pm10","total","alarms","errors")
x$elapsedtime<-x$elapsedtime+startDate
x<-x[,-c(7,8)] #remove the alarms and errors variables
names(x$elapsedtime)<-"datetime" #rename timestamp to datetime
return(x)
}
read.dtrak("**INSERT FILE PATH HERE**")发布于 2017-03-03 23:50:37
这是一个非常简单的问题,假设每个文件的标题中都有固定的行数。POSIXct对象是从开始到现在的秒数。因为您的数据是以秒为单位的,所以只是将经过的时间添加到启动时间上。
我读了这两行的开始日期和时间。将值粘贴到一起并转换为datetime对象,然后读取其余的数据。将经过的时间添加到启动时间中,您就可以开始了。
#pratice<-readLines("dusttrak_data.csv")
#get start time and date then convert to POSIXct object
stime<-read.csv("dusttrak_data.csv", header = FALSE, nrow=1, skip=6)
sdate<-read.csv("dusttrak_data.csv", header = FALSE, nrow=1, skip=7)
#read data, and add elasped time to start time
startDate<-as.POSIXct(paste(sdate$V2, stime$V2), "%m/%d/%Y %I:%M:%S %p", tz="EST")
df<-sdate<-read.csv("dusttrak_data.csv", skip=36)
names(df)<-c("elapsedtime", "PM1", "PM2.5", "PM4", "PM10", "TOTAL", "Alarms", "Errors")
df$elapsedtime<-df$elapsedtime+startDate
#removed columns 7 and 8
df<-df[,-c(7:8)]您需要调整as.POSIXct函数中的时区以匹配传感器的时间。
https://stackoverflow.com/questions/42590066
复制相似问题