R Sample 2

# This is a function to check if the input is a leap year or not

leapCheck <- function(data) { numofdata <-length(data) result <-c() for (i in 1:numofdata){ if (data[i] %% 4 == 0 & data[i] %% 100 == 0 & data[i] %% 400 == 0) result[i] <-TRUE else if (data[i] %% 4 == 0 & data[i] %% 100 == 0) result[i] <-FALSE else if (data[i] %% 4 == 0) result[i] <-TRUE else result[i] <-FALSE } col_headings <-c('Year', 'Leap Year?') tableresult <-data.frame(data, result) names(tableresult) <-col_headings return(tableresult) }

# > leapCheck(2000) returns a data frame # Year Leap Year? # 1 2000 TRUE # > Year <-c(2010:2020) # > leapCheck(Year) # Year Leap Year? # 1 2010 FALSE # 2 2011 FALSE # 3 2012 TRUE # 4 2013 FALSE # 5 2014 FALSE # 6 2015 FALSE # 7 2016 TRUE # 8 2017 FALSE # 9 2018 FALSE # 10 2019 FALSE # 11 2020 TRUE