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,130 points

1 answer to this question.

0 votes

Consider a data frame like this:

#Create DF
 year<-rep(c(2004:2014), each=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 )

df2 <- df %>% filter(!(year %% 4 > 0 & day==366))
answered May 11, 2018 by darklord
• 6,170 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,690 points
0 votes
0 answers

Join 2 columns based on condition in R

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

Jul 1 in Data Analytics by nikita
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 in Data Analytics by Gups_1985
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 in Data Analytics by anonymous
• 31,840 points
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 26, 2018 in Data Analytics by darklord
• 6,170 points
0 votes
1 answer

Filter using dplyr

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

answered Sep 10 in Data Analytics by kiran
0 votes
1 answer

Big Data transformations with R

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

answered Dec 17, 2017 in Data Analytics by Sudhir
• 1,610 points
0 votes
1 answer

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 darklord
• 6,170 points
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 darklord
• 6,170 points