Skip to content

Instantly share code, notes, and snippets.

@floswald
Created September 2, 2011 11:08
Show Gist options
  • Save floswald/1188384 to your computer and use it in GitHub Desktop.
Save floswald/1188384 to your computer and use it in GitHub Desktop.
hits on website
# load data
hdata <- read.csv("helpdesk log.csv",header=TRUE,sep=",")
hdata$date <- as.Date(hdata$Timestamp,format="%m/%d/%Y") # Timestamp is in mm/dd/YYYY format
daycount <- table(hdata$date) # count how many hits per day
# daycount is what I call the "short" vector. It misses some days.
##################################
# daily response wrong graph
oldmar <- c(5,4,4,2) + 0.1 # store default pars
par(mar = c(7, 4, 4, 2) + 0.1) # setup bigger plotting window
png(file="daily-wrong.png")
plot(daycount,xaxt="no",type="l",main="Daily number of Interactions",ylab = "Number of Interactions",xlab="")
axis(1,at=seq(1,44,by=6),labels=FALSE)
labs <- names(daycount)
text(seq(1,44,by=6), par("usr")[3] - 0.5, srt = 45, adj = 1, # text() allows to print axis labels rotated (srt=45)
labels = labs[seq(1,44,by=6)], xpd = TRUE)
mtext("Date",side=1,line = 5)
grid()
dev.off()
par(mar = oldmar)
##################################
# generate vector of all dates
alldays <- seq(hdata$date[1],length=62,by="+1 day") # this vector has all days
# notice how R recognizes the date format and chooses the appropriate method for the seq() function.
# You don't have to worry about 30 days in June, 31 in July etc. R does it all for you.
allcount <- table(alldays) # create table object from alldays.
actindex <- match(names(allcount),names(daycount),nomatch = 0)
# create "active" index: vector of length(allcount), i.e. all days.
# on days with no activity (i.e. a missing day in daycount), this has value 0 (nomatch = 0).
# For days with activity, actindex holds the index of the matching position in daycount.
# function to get entries of daycount corresponding to actindex
# indexing is a bit tricky. i loops over all days. get correct date by
# substracting all "zero-activity" days accumulated so far.
days <- function(actindex,daycount){
n <- length(actindex)
x <- rep(NA,times=n)
zero <- 0
for (i in 1:n){
if (actindex[i]==0) {
zero <- zero +1
x[i] <- 0
} else {
x[i] <- daycount[i-zero]
}
}
return(x)
}
alldaycount <- array(days(actindex,daycount)) # construct vector with number of hits per day
names(alldaycount) <- names(allcount) # name entries by consecutive dates.
##################################
# daily response: correct graph
png(file="dailyrespons.png") # save next graph as .png
par(mar = c(7, 4, 4, 2) + 0.1)
plot(alldaycount,axes=FALSE,type="l",main="Daily number of Interactions",ylab = "Number of Interactions",xlab="")
axis(1,at=seq(1,62,by=5),labels=FALSE)
axis(2,at=c(0,1,2,3,4,5,8))
labs <- names(alldaycount)[seq(1,62,by=5)]
text(seq(1,62,by=5), par("usr")[3] - 0.5, srt = 45, adj = 1,
labels = labs, xpd = TRUE)
mtext("Date",side=1,line = 5)
grid()
dev.off()
par(mar = oldmar)
##################################
@Vamshi-dhar
Copy link

Vamshi-dhar commented Aug 24, 2017

Hi floswald,
I guess this will be helpfull
`

Finding and Eliminating missing values

hdata <- data.frame(
  date = seq(as.Date('1988-04-20'),by ='days' , length = 100),
  hits = sample(rnorm(100, mean = 110, sd =11),100)
)

Deleted 5 observations randomly:

hdata <- hdata[sample(1:100,95,replace = F),]
hdata <- hdata[order(hdata$date),]
hdata$date <- as.Date(hdata$date,format="%m/%d/%Y")	# Timestamp is in mm/dd/YYYY format
## Observed data plot
plot(hdata$hits,xaxt="no",type="l",main="Daily number of Interactions",ylab = "Number of Interactions",xlab="")

generate vector of all dates

daycount <- table(hdata$date)  
alldays <- seq(as.Date('1988-04-20'),length=100,by="+1 day")			# this vector has all days
allcount <- table(alldays)      
actindex <- match(names(allcount),names(daycount),nomatch = 0)  

Now we can see here there are 5 missing values:

Lets mention the missing dates and provide hits as zero [0] for missing dates

library(dplyr)
alldays <- data.frame(date = alldays)
new_hdata <-  left_join(alldays, hdata)
new_hdata$hits[new_hdata$hits %in% NA] <- 0
## Adjusted data plot 
plot(new_hdata$hits,xaxt="no",type="l",main="Daily number of Interactions",ylab = "Number of Interactions",xlab="")

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment