tmnas.blogg.se

Sapply for loop in r
Sapply for loop in r





sapply for loop in r

Vapply is similar to sapply, but has a pre-specified type of return value, so it can be safer (and sometimes faster) to use. sapply(x, f, simplify = FALSE, USE.NAMES = FALSE) is the same as lapply(x, f). Sapply is a user-friendly version and wrapper of lapply by default returning a vector, matrix or, if simplify = “array”, an array if appropriate, by applying simplify2array(). Lapply returns a list of the same length as X, each element of which is the result of applying FUN to the corresponding element of X. So they are more like replacement for loop Output item x1970.a x1970.Hey- these are not really loop functions, they are special functions to apply functions. 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.

sapply for loop in r

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.

sapply for loop in r

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.

sapply for loop in r

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. I'm working with a dataset that shows which variables exist in each year.







Sapply for loop in r