#installing and loading neccessary packages and setting work directory install.packages("chron") #setwd("....") library(chron) library(base) rm(list=ls()) #creating 15 minute time points from 10/01/1990 to 08/15/2014 dates = seq.dates("10/01/1990", "08/15/2014", by = "days") dates = as.character(as.Date(dates, format = "%m/%d/%y")) daynum = seq(1:length(dates)) times = as.character(times(0:95/96)) Dates = rep(dates, each = length(times)) DayNum = rep(daynum, each = length(times)) Times = rep(times, length(dates)) Times = paste(substr(Times,1,5)) Timepts = paste(Dates, Times, sep="") Timepts = as.numeric(gsub("-", "", gsub(":", "",Timepts))) #putting timepts in a data frame Data = data.frame(DayNum,Dates,Times,Timepts) #reading IDA from data files ida.old = read.table("idaold.txt", header = FALSE, sep="", colClasses = "character") ida.new = read.table("idanew.txt", header = FALSE, sep="", na.strings = "NA", colClasses = "character") #putting IDA timepts in same format as "Data" #old Ida.Old = cbind(paste(substr(ida.old[,1],1,12)), ida.old[,2]) #new gsub1 = gsub("-", "", paste(ida.new[,1],ida.new[,2])) gsub1 = gsub(" ", "", gsub1) gsub1 = gsub(":", "", gsub1) Ida.New = cbind(paste(gsub1), ida.new[,3]) #merging the two IDA data last = Ida.Old[nrow(Ida.Old),1] start = as.numeric(which(Ida.New[,1]==last)) + 1 Ida.New = Ida.New[start:nrow(Ida.New),] Ida = rbind(Ida.Old, Ida.New) IDA = cbind(as.numeric(Ida[,1]), as.numeric(Ida[,2])) #writing a file to put readings of river flow in appropriate order #note that it takes a while to write this file ("cfs.txt") cfs = rep("NA", nrow(Data)) for(i in 1:nrow(Data)){ p = which(Data[,4]==IDA[i,1]) cfs[p] = IDA[i,2] } write(cfs, "cfs.txt") #writing in a ready to use data form - "Data.txt" cfs = read.table("cfs.txt", header = FALSE) Data = cbind(Data,cfs) names(Data) = c("DayNum","Dates","Times","Timepts","cfs") write.table(Data, "Data.txt", sep="\t")