##### Lesson 11: Fran, the functionin' R-bot ##### # Setting the working directory setwd(“give the path to the folder“) list.files() # structure of a function # functionname = function(inputs) { instructions return(output) } # function to add two numbers # add = function (a, b) { y = a + b # instruction return (y) # the function "add" will take two numbers a and b and return y } # test the function # add (10, 15) # function for line equation # line_eq = function (m, x, b) { # m is the slope of the line # b is the intercept of the line # x is the point on the x-axis y = m*x + b # equation for the line return(y) } # test the function # line_eq(0.5, 5, 10) # function for line equation + (x + y) # two_tasks = function (m, x, b) { # m is the slope of the line # b is the intercept of the line # x is the point on the x-axis y = m*x + b # equation for the line z = x + y return(c(y,z)) } # test the function # two_tasks(0.5, 5, 10) # use on vectors # x = c(1,2,3,4,5) m = 0.5 b = 10 line_eq(m,x,b) # function to analyze bike data # bike_analysis = function(bike_data,station_name) { dum = which (bike_data$Start.Station.Name == station_name & bike_data$End.Station.Name == station_name) total_rides = length(dum) average_time = mean(bike_data$Trip.Duration[dum])/60 # in minutes max_time = max(bike_data$Trip.Duration[dum])/60 # in minutes min_time = min(bike_data$Trip.Duration[dum])/60 # in minutes output = c(total_rides,average_time,max_time,min_time) return(output) } # use the function to analyze Central Park South Loop # # bike data # bike_data = read.csv("201703-citibike-tripdata.csv",header=T) station_name = "Central Park S & 6 Ave" bike_analysis(bike_data,station_name)