Any filter based on conditional criteria in r

0 votes
I would like to subset based on a specific criteria - any sort of conditional filter.

My data frame is a panel dataset of daily values for each day between 2004-2014.

Each day in the data frame is a separate observation. Each year has 366 days. I would like to subset the data such that only the leap years retain the 366th day in the panel data.

I need a script that will return a dataset without the 366th day but only for each year other than 2004, 2008, and 2012.

Can someone help me out with this?
May 11, 2018 in Data Analytics by DataKing99
• 8,240 points
1,977 views

1 answer to this question.

0 votes

Consider a data frame like this:

#Create DF
 year<-rep(c(2004:2014), each=366)
 day<-rep(c(1:366))
 df<-data.frame(day, year)

Set up data:

df  <- expand.grid(year=2004:2014,day=1:366)
nrow(df) ## 4026

Now exclude cases where (the year is not divisible by 4) AND (day equals 366) (identifying non-leap years would be trickier if you included 2000 and/or century-years in your dataset )

library(dplyr)
df2 <- df %>% filter(!(year %% 4 > 0 & day==366))
answered May 11, 2018 by Sahiti
• 6,370 points

Related Questions In Data Analytics

0 votes
1 answer

How to create dummy variables based on a categorical variable of lists in R?

You can use mtabulate in the following way: library(qdapTools) cbind(data[1], ...READ MORE

answered Apr 13, 2018 in Data Analytics by CodingByHeart77
• 3,740 points
2,324 views
0 votes
0 answers

Join 2 columns based on condition in R

How to join 2 columns in R ...READ MORE

Jul 1, 2019 in Data Analytics by nikita
2,128 views
0 votes
1 answer

Sort a field based on another field in R

Hi.. Just for sorting, you can use arrange ...READ MORE

answered Aug 29, 2019 in Data Analytics by Gups_1985
605 views
0 votes
2 answers

How to remove rows with missing values (NAs) in a data frame?

Hi, The below code returns rows without ...READ MORE

answered Aug 20, 2019 in Data Analytics by anonymous
• 33,030 points
14,441 views
0 votes
1 answer

How to subset data so that it contains only columns whose names match a condition

You can use grepl on the names ...READ MORE

answered Apr 27, 2018 in Data Analytics by Sahiti
• 6,370 points
6,049 views
0 votes
1 answer

Filter using dplyr

@Sagar, use filter followed by condition for ...READ MORE

answered Sep 10, 2019 in Data Analytics by kiran
716 views
0 votes
1 answer

Big Data transformations with R

Dear Koushik, Hope you are doing great. You can ...READ MORE

answered Dec 18, 2017 in Data Analytics by Sudhir
• 1,610 points
768 views
+1 vote
2 answers

How to replace a value in a data frame based on a conditional 'If' statement?

It's easier to convert alpha to characters ...READ MORE

answered Jun 6, 2018 in Data Analytics by Sahiti
• 6,370 points
36,315 views
0 votes
1 answer

How to change y axis max in time series using R?

The axis limits are being set using ...READ MORE

answered Apr 3, 2018 in Data Analytics by Sahiti
• 6,370 points
3,541 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP