R programming: leap years calculation

0 votes

I'm creating a function that returns leap years. However, my code returns the results in the form of logical vector. How can I get the results in numeric vector. For example, for the below code the result should be "c(2000,2004,2008)"

    "ans<- function(year1, year2)
 {return(((c(year1:year2) %% 4 == 0) & (c(year1:year2) %% 100 != 0)) | (c(year1:year2) %% 400 == 0)); return(c(year1:year2))}"

 ans(2000,2010)

TRUE FALSE FALSE FALSE TRUE FALSE FALSE FALSE TRUE FALSE FALSE

Feb 20 in Data Analytics by Tyrion anex
• 8,280 points
77 views

1 answer to this question.

0 votes

Here's a small modification to your code, try this, it should work:

ans<- function(year1, year2){
  return(c(year1:year2)[((c(year1:year2) %% 4 == 0) & (c(year1:year2) %% 100 != 0)) | (c(year1:year2) %% 400 == 0)])}

> ans(2000, 2010)
[1] 2000 2004 2008
answered Feb 20 by Sophie may
• 9,370 points

Related Questions In Data Analytics

0 votes
1 answer

What are the rules to define a variable name in R programming language?

A variable name in R programming language ...READ MORE

answered Sep 7, 2018 in Data Analytics by zombie
• 3,690 points
69 views
0 votes
1 answer

Why should I adopt R programming

R Programming is the best mechanism for ...READ MORE

answered Oct 29, 2018 in Data Analytics by Maverick
• 10,040 points
16 views
0 votes
1 answer

Error: could not find function - R Programming

This error usually occurs when a package ...READ MORE

answered Oct 29, 2018 in Data Analytics by Maverick
• 10,040 points
271 views
0 votes
1 answer

R programming Web Scraping

Try something like this: library(rvest) library(rvest) library(tidy ...READ MORE

answered Oct 29, 2018 in Data Analytics by Maverick
• 10,040 points
50 views
+1 vote
2 answers
0 votes
1 answer

R programming logic

Use gsub to match the substring that we want ...READ MORE

answered Nov 16, 2018 in Data Analytics by Maverick
• 10,040 points
21 views
0 votes
1 answer
+4 votes
2 answers
0 votes
1 answer

R Programming: matrices

Try this, It will test if a matrix ...READ MORE

answered Dec 17, 2018 in Data Analytics by Sophie may
• 9,370 points
24 views
0 votes
1 answer

R programming: Unexpected symbol error

Format your code this way: myfunction <- function() ...READ MORE

answered Dec 17, 2018 in Data Analytics by Sophie may
• 9,370 points
157 views