'Select columns with for loop

Suppose you have a data frame like this (data at the end)

And you make a function to filter and select.

Where data is the data frame, start="2003 Q1", and end could be any element of the dates vector.

So this is the function:

help_god <- function(data, start, end){
  
  dates <- c("2011 Q4","2012 Q4", "2013 Q4", "2014 Q4", "2015 Q4", "2016 Q4", "2017 Q4", "2018 Q4")
  
  data <- data %>% 
    filter(quarter>=as.yearqtr(start),
           quarter<=as.yearqtr(end))
  
  for(i in 1:length(dates)){
    data <- data %>% 
      select(quarter, contains(paste0("t_",10+i)))
  }
  
  
  return(data)
  
  
}

So first, the function filters the data and keep the rows from the start quarter to the end quarter. But then, I want to select the columns that contain the suffix

So

if end=="2011 Q4" should contain quarter and all the columns that end in t_11

if end=="2012 Q4" should contain quarter and all the columns that end in t_12

and so on to

if end=="2019 Q4" should contain quarter and all the columns that end in t_20

I think there's something wrong with the for loop because it just gets the quarter columns

Thanks in advance!!

Data:

set.seed(1)
example2 <- data.frame(
  quarter=seq(as.Date(as.yearqtr("2003 Q1")),by="quarters", length=72),
  t_edg_gc_lt_11=sample(1:72),
  t_edg_gc_lt_12=sample(1:72),
  t_edg_gc_lt_13=sample(1:72),
  t_edg_gc_lt_14=sample(1:72),
  t_edg_gc_lt_15=sample(1:72),
  t_edg_gc_lt_16=sample(1:72),
  t_edg_gc_lt_17=sample(1:72),
  t_edg_gc_lt_18=sample(1:72),
  t_edg_gc_lt_19=sample(1:72),
  t_edg_gc_lt_20=sample(1:72),
  t_edg_gk_lt_11=sample(1:72),
  t_edg_gk_lt_12=sample(1:72),
  t_edg_gk_lt_13=sample(1:72),
  t_edg_gk_lt_14=sample(1:72),
  t_edg_gk_lt_15=sample(1:72),
  t_edg_gk_lt_16=sample(1:72),
  t_edg_gk_lt_17=sample(1:72),
  t_edg_gk_lt_18=sample(1:72),
  t_edg_gk_lt_19=sample(1:72),
  t_edg_gk_lt_20=sample(1:72)) %>% mutate(quarter=as.yearqtr(quarter))


Solution 1:[1]

It seems like you're just trying to subset your data based on the inputs. In that case perhaps the following will work.

library(tidyverse)
library(zoo)
library(lubridate)

set.seed(1)
example2 <- data.frame(
  quarter=seq(as.Date(as.yearqtr("2003 Q1")),by="quarters", length=72),
  t_edg_gc_lt_11=sample(1:72),
  t_edg_gc_lt_12=sample(1:72),
  t_edg_gc_lt_13=sample(1:72),
  t_edg_gc_lt_14=sample(1:72),
  t_edg_gc_lt_15=sample(1:72),
  t_edg_gc_lt_16=sample(1:72),
  t_edg_gc_lt_17=sample(1:72),
  t_edg_gc_lt_18=sample(1:72),
  t_edg_gc_lt_19=sample(1:72),
  t_edg_gc_lt_20=sample(1:72),
  t_edg_gk_lt_11=sample(1:72),
  t_edg_gk_lt_12=sample(1:72),
  t_edg_gk_lt_13=sample(1:72),
  t_edg_gk_lt_14=sample(1:72),
  t_edg_gk_lt_15=sample(1:72),
  t_edg_gk_lt_16=sample(1:72),
  t_edg_gk_lt_17=sample(1:72),
  t_edg_gk_lt_18=sample(1:72),
  t_edg_gk_lt_19=sample(1:72),
  t_edg_gk_lt_20=sample(1:72)) %>% mutate(quarter=as.yearqtr(quarter))


help_god <- function(data, start, end){
  # pull desired ending from end input
  end_yr <- end %>% as.yearqtr() %>% year() %>% (function(x){x-2000}) %>% as.character()
  # filter and select based on inputs
  data %>% 
    filter(quarter>=as.yearqtr(start),
           quarter<=as.yearqtr(end)) %>% 
    select(quarter, ends_with(end_yr))

}

# possible dates
dates <- c("2011 Q4","2012 Q4", "2013 Q4", "2014 Q4", "2015 Q4", "2016 Q4", "2017 Q4", "2018 Q4")
# pick some for demo
start <- dates[2]
end <- dates[5]

# run function
help_god(data = example2, start = start, end = end)
#>    quarter t_edg_gc_lt_15 t_edg_gk_lt_15
#> 1  2012 Q4             17              9
#> 2  2013 Q1             24             25
#> 3  2013 Q2              9             72
#> 4  2013 Q3             40             63
#> 5  2013 Q4             14             45
#> 6  2014 Q1             31             40
#> 7  2014 Q2             41             53
#> 8  2014 Q3             22             30
#> 9  2014 Q4              8             62
#> 10 2015 Q1              3             56
#> 11 2015 Q2             63             67
#> 12 2015 Q3             44             55
#> 13 2015 Q4             66             33

Created on 2022-02-02 by the reprex package (v2.0.1)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dan Adams