c— title: “Week2 summary” author: “haha” date: “20 January 2018” output: html_document —
#########season function
Season <- function(data2) {
d<- as.Date(strptime(data2$Start.Date,format="%d/%m/%Y %H:%M",tz="UTC"))
WS <- as.Date("21/12/2016", format = "%d/%m/%Y") # Winter
SE <- as.Date("20/3/2016", format = "%d/%m/%Y") # Spring
SS <- as.Date("21/6/2016", format = "%d/%m/%Y") # Summer
FE <- as.Date("22/9/2016", format = "%d/%m/%Y") # Fall
ifelse (d >= WS | d < SE, "Winter",
ifelse (d >= SE & d < SS, "Spring",
ifelse (d >= SS & d < FE, "Summer", "Fall")))
}
seasonweekdata<- function(data1,coordinates=coor){
data1<-data1 %>% select("StartStation.Id","EndStation.Id","Start.Date")
###merge coordinate
#########################################################################
data1$season<- Season(data2 = bikedata)
a<- coor
colnames(a)<- c("StartStation.Id","s.lat","s.lon")
x<-join(data1,a)
aa<- coor
colnames(aa)<- c("EndStation.Id","e.lat","e.lon")
#a<-setnames(coor,old=c("id","lat","long"),new=c("EndStation.Id","e.lat","e.lon"))
#aa<-setnames(coor,old=c("id","lat","long"),new=c("StartStation.Id","s.lat","s.lon"))
z<-join(x,aa)
head(z)
z<- na.omit(z)
x<- count(z,vars = c("StartStation.Id","EndStation.Id"))
data1<- join(z,x)
#################weekdays############################################################
data1$weekdays<-weekdays(
strptime(data1$Start.Date,format="%d/%m/%Y %H:%M",tz="UTC")
)
########################################seasons
head(data1)
return(data1)
##########same start and end stations #########################################
#attach(data1)
# data1$samestation<- rep(0,nrow(data1))
#head(data1)
#data1$samestation[which((StartStation.Id==EndStation.Id))]<- data1$freq[which((StartStation.Id==EndStation.Id))]
}
str(seasonweekdata(data1 = bikedata))
## Joining by: StartStation.Id
## Joining by: EndStation.Id
## Joining by: StartStation.Id, EndStation.Id
## 'data.frame': 223507 obs. of 10 variables:
## $ StartStation.Id: int 706 587 405 587 577 270 109 527 191 109 ...
## $ EndStation.Id : int 308 587 432 587 495 270 749 133 634 196 ...
## $ Start.Date : Factor w/ 9704 levels "21/06/2017 00:00",..: 1 1 1 1 1 2 2 2 2 2 ...
## $ season : chr "Winter" "Winter" "Winter" "Winter" ...
## $ s.lat : num 51.5 51.5 51.5 51.5 51.5 ...
## $ s.lon : num -0.0836 -0.085 -0.1827 -0.085 -0.0475 ...
## $ e.lat : num 51.5 51.5 51.5 51.5 51.5 ...
## $ e.lon : num -0.0857 -0.085 -0.1739 -0.085 -0.0187 ...
## $ freq : int 3 5 6 5 3 1 3 4 2 1 ...
## $ weekdays : chr "Wednesday" "Wednesday" "Wednesday" "Wednesday" ...
fullplot<- function(data,type="nothing", source="google",maptype="roadmap",zoom = 11,
lowcol="grey90",highcol="black"){
map<- get_map(location = "London",source="google",maptype="roadmap",zoom = 11)
plot<-ggmap(map)+
geom_segment(data=data,aes(x=s.lon, y=s.lat, xend=e.lon, yend=e.lat, colour=freq),
size=0.00001,alpha=0.3)+
xlim(range(data$s.lon))+
ylim(range(data$s.lat))+
scale_color_gradient(low=lowcol,high=highcol)+
geom_point(data=data,aes(x=s.lon,y=s.lat),size=0.001,alpha=0.3)
if (type== "weekdays"){
plot<- plot+ facet_wrap(~ weekdays)
}
else if ( type == "season"){
attach(data)
plot<- plot+ facet_wrap(~ season)
}
else{
plot <- plot
}
plot
}
fullplot(data=seasonweekdata(data1=bikedata),lowcol = "grey90",highcol = "black")
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=London&zoom=11&size=640x640&scale=2&maptype=roadmap&language=en-EN&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=London&sensor=false
## Joining by: StartStation.Id
## Joining by: EndStation.Id
## Joining by: StartStation.Id, EndStation.Id
## Scale for 'x' is already present. Adding another scale for 'x', which
## will replace the existing scale.
## Scale for 'y' is already present. Adding another scale for 'y', which
## will replace the existing scale.
fullplot(data=seasonweekdata(data1=bikedata),lowcol = "wheat",highcol = "darkblue",type = "weekdays")
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=London&zoom=11&size=640x640&scale=2&maptype=roadmap&language=en-EN&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=London&sensor=false
## Joining by: StartStation.Id
## Joining by: EndStation.Id
## Joining by: StartStation.Id, EndStation.Id
## Scale for 'x' is already present. Adding another scale for 'x', which
## will replace the existing scale.
## Scale for 'y' is already present. Adding another scale for 'y', which
## will replace the existing scale.
fullplot(data=seasonweekdata(data1=bikedata),lowcol = "wheat",highcol = "black",type = "season")
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=London&zoom=11&size=640x640&scale=2&maptype=roadmap&language=en-EN&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=London&sensor=false
## Joining by: StartStation.Id
## Joining by: EndStation.Id
## Joining by: StartStation.Id, EndStation.Id
## Scale for 'x' is already present. Adding another scale for 'x', which
## will replace the existing scale.
## Scale for 'y' is already present. Adding another scale for 'y', which
## will replace the existing scale.