I'm working with a dataset that shows which variables exist in each year. I need a function that will allow me to specify certain years and then return the variables that appear in ALL of those years. In other words, if specify years "70" and "90", I want the function to find all rows with variables in **both** of those years (based on the variable names, not the column names).

[This post](https://stackoverflow.com/questions/34914173/using-grepl-to-extract-a-value-from-text-across-multiple-columns) gave me the idea to paste all of the year columns together so I only have to apply grepl() to one column. As you can see below, the only way I could figure to match ALL years was to run the `for` loop twice, once to identify any matches on year and a second time to remove any non-matches. Is there a simpler way to do this?  

I'm relatively new to R (and this is my first post here!) so I appreciate any ideas.

```
# testing data frame (actual data has many more items and years)
item <- c("ABC", "DEF", "GHI", "JKL", "MNO", "PQR", "STU")
x1970 <- c("abc70", "70def", "gh70i", "jkl70", "", "70pqr", "stu70x")
x1980 <- c("abc80", "80def", "", "", "", "80pqr", "stu80x")
x1990 <- c("abc90", "90def", "", "", "90mno90", "90pqr", "")
x2000 <- c("", "00def", "gh00i", "jkl00", "00mno00", "00pqr", "")
df <- data.frame(item, x1970, x1980, x1990, x2000)

df
  item  x1970  x1980   x1990   x2000
1  ABC  abc70  abc80   abc90        
2  DEF  70def  80def   90def   00def
3  GHI  gh70i                  gh00i
4  JKL  jkl70                  jkl00
5  MNO               90mno90 00mno00
6  PQR  70pqr  80pqr   90pqr   00pqr
7  STU stu70x stu80x 

year_filter_test <- function(data, year)
{
  # paste all year columns together in "search_columns" so I can apply grepl() to that one column
  data2 <- data %>% mutate(search_columns = paste(x1970, x1980, x1990, x2000) )

  # create "include" column which will be TRUE if I want to include this row
  data2$include <- rep( "", nrow(data2))

  # loop through each year, making include column TRUE if there's a match on ANY year
  for (i in year)
  { data2$include [ grepl(i, data2$search_columns) ] <- TRUE
  }

  # loop through each year again, making include column FALSE if there's NOT a match on ANY year
  # the effect of these two loops is to only include columns that match ALL years
  for (i in year)
  { data2$include [ !grepl(i, data2$search_columns) ] <- FALSE
  }

  selected.years <- data2 %>% filter(include==TRUE)
  return(selected.years)
}

```