Tags

, , , , , ,

weather<- read.csv(“M4_not_cleaned1.csv”, header=TRUE)
new.weather<- weather[,2:3]

new.weather$date<- lapply(strsplit(as.character(new.weather$time_UTC), “\\T”), “[“, 1)
new.weather$time1<- lapply(strsplit(as.character(new.weather$time_UTC), “\\T”), “[“, 2)

new.weather$time2<- lapply(strsplit(as.character(new.weather$time1), “\\Z”), “[“, 2)
new.weather$time<- lapply(strsplit(as.character(new.weather$time1), “\\Z”), “[“, 1)

new.weather<- new.weather[,-4:-5]
new.weather<- new.weather[,-1]

new.weather<- new.weather[,c(2,3,1)]

new.weather$DateTime <- paste(new.weather$date,new.weather$time, sep = ” “)
new.weather<- new.weather[,c(4,3)]

############################################################################
new.weather1<- new.weather
isna<- is.na(new.weather1)
mean1<- mean(new.weather1$AtmosphericPressure_mb, na.rm=T)
new.weather1$AtmosphericPressure_mb[which(is.nan(new.weather1$AtmosphericPressure_mb))] = mean1

library(zoo)
nwstat <- function(x) c(min = min(x), max = max(x), mean = mean(x))
z <- data.frame(read.zoo(new.weather1, header = TRUE, sep = “,”, format = “%Y-%m-%d”, aggregate = nwstat))
head(z)
meanz<- apply.daily(z,mean)

isnan<- which(is.na(meanz))
isnan
# found no NA or NaN

plot(meanz$mean)

plot1

# the plot is really bad… which means there are many NAs in sequence in the time series data,
# its better if we completely remove them instead of replacing with mean of the series without NAs
# so we go back…

rm(list=ls())
# Another lesson learnt

############################ Rework #######################################33

weather<- read.csv(“M4_not_cleaned1.csv”, header=TRUE)
new.weather<- weather[,2:3]

new.weather$date<- lapply(strsplit(as.character(new.weather$time_UTC), “\\T”), “[“, 1)
new.weather$time1<- lapply(strsplit(as.character(new.weather$time_UTC), “\\T”), “[“, 2)

new.weather$time2<- lapply(strsplit(as.character(new.weather$time1), “\\Z”), “[“, 2)
new.weather$time<- lapply(strsplit(as.character(new.weather$time1), “\\Z”), “[“, 1)

new.weather<- new.weather[,-4:-5]
new.weather<- new.weather[,-1]

new.weather<- new.weather[,c(2,3,1)]

new.weather$DateTime <- paste(new.weather$date,new.weather$time, sep = ” “)
new.weather<- new.weather[,c(4,3)]

new.weather1<- new.weather

new.weather2<- new.weather[complete.cases(new.weather1),]
row.names(new.weather2) <- NULL

isnan<- which(is.na(new.weather2))
isnan
# found no NA or NaN… great!
# Now we calculate mean of daily values

library(xts)
library(zoo)
# writing a function to calculate Minimum, Maximum and Mean values of the Atm Press. on daily basis
new.weather.fn <- function(x) c(min = min(x), max = max(x), mean = mean(x))
new.weather.df <- data.frame(read.zoo(new.weather2, header = TRUE, sep = “,”, format = “%Y-%m-%d”, aggregate = new.weather.fn))
# Check the results
head(new.weather.df)
row.names(new.weather.df) <- NULL

################################################################################################
#same can also be produced using apply.daily() function in R under xts & zoo library:
try1<- apply.daily(new.weather.df, mean)
# an extra column is generated, so remove it
row.names(try1) <- NULL
################################################################################################

# Generating a time series data of daily mean of Atm Prs
library(tseries)
library(forecast)
library(TTR)

Atm.Prs.TS <- ts(new.weather.df$mean, frequency=1)
plot.ts(Atm.Prs.TS)
# This plot looks better than earlier one

plot2

 

 

#Now save this data in a csv format in current working directory for later use

write.table(Atm.Prs.TS, file=”atmprsts.csv”, sep=”,”)

 

Good Luck ! Practice it well…

Manoj Kumar

 

Advertisements