I created example data with 1970.a and 1970.b columns.Įxample Data df <- structure(list(item = c("ABC", "DEF", "GHI", "JKL", "MNO", "PQR", year_filter %įilter(grepl(paste(last2dig_yrs, collapse = "|"), value)) %>% In the end, you will have row numbers of data to return based on what remains after the filter. Then, grouping by each row of the ame, filter first by including matches with the last 2 digits of the year, and then making sure all years are found in the row. Put your data into long format, and extract the years from the column names (so that 1970.a and 1970.b would be considered for 1970).
#Sapply for loop in r full
First, allow the function to use full 4 digit years, and create a variable to store the last 2 digits for searching. library(tidyverse)įunction(x) grepl(paste(years, collapse = "|"), x))Įdit: Given that there may be more than one column per year, the column year should be considered in the function. If the sum is the same as the number of "years" that you are searching for, then that row will be retained in your filter. If that is correct, you could use rowSums to add up the partial matches found for character values in a given row. This was a bit unclear, but it sounds like you're looking to match the values in your ame with your function, which contain the last 2 digits of years, and not look at the column name which also contains years. If we want to filter directly, we can modify the function: my_filter%įilter(if_all(num_range('x', years), ~str_detect(tolower(.x), tolower(item)))) We can easily use the new logical column to perform filtering operations. We can use dplyr with rowwise, if_all, and stringr::str_detect: library(dplyr) # create "include" column which will be TRUE if I want to include this row # testing data frame (actual data has many more items and years) I'm relatively new to R (and this is my first post here!) so I appreciate any ideas. 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. This post gave me the idea to paste all of the year columns together so I only have to apply grepl() to one column. 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). I need a function that will allow me to specify certain years and then return the variables that appear in ALL of those years. Suppose you want to do several printouts of the following form: The year is where is equal to 2010, 2011, up to 2015.I'm working with a dataset that shows which variables exist in each year. Let’s get back to the conceptual meaning of a loop. If you want to learn more on the concepts of vectorization in R, this is a good read.
#Sapply for loop in r how to
Nevertheless, as a beginner in R, it is good to have a basic understanding of loops and how to write them. For example, solutions that make use of loops are less efficient than vectorized solutions that make use of apply functions, such as lapply and sapply. Simply put, this allows for much faster calculations. Why? Well, that’s because R supports vectorization. When surfing on the web you’ll often read that one should avoid making use of loops in R. Sounds weird? No worries, it will become more clear once we start working with some examples below.īefore you dive into writing loops in R, there is one important thing you should know.
#Sapply for loop in r code
They allow you to automate parts of your code that are in need of repetition. Ĭonceptually, a loop is a way to repeat a sequence of instructions under certain conditions. It is aimed at beginners, and if you’re not yet familiar with the basic syntax of the R language we recommend you to first have a look at this introductory R tutorial. In this tutorial we will have a look at how you can write a basic for loop in R.