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,100 points
28 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 darklord
• 6,140 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,680 points
402 views
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
8 views
0 votes
0 answers

Sort a field based on another field in R

Hi, I want to sort the students ...READ MORE

3 days ago in Data Analytics by priya
6 views
0 votes
1 answer

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

You can use complete.cases in the following ...READ MORE

answered Apr 13, 2018 in Data Analytics by darklord
• 6,140 points
3,681 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 26, 2018 in Data Analytics by darklord
• 6,140 points
25 views
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
35 views
0 votes
2 answers

Transforming a key/value string into distinct rows in R

We would start off by loading the ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,550 points
46 views
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,140 points
33 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 darklord
• 6,140 points
69 views