I have a very simple sample data frame df_test as:
df_test <- data.frame("A" = 1:5)
I would like to select the row containing 5. I know I can achieve it by using the filter() command as:
df_analysis <- df_test %>%
filter(A == 5)
However, I want to run a for loop (as the actual data set has many variables and is complex), thus instead of filtering columns manually one by one, I would like to run a for loop of columns that can pick one variable at a time and filter rows accordingly. For this example, I create a character vector v as v = c("A").
Now to filter, instead of using the column name, when I try to use this vector index as:
df_analysis <- df_test %>%
filter(v[1] == 5)
It produces 0 rows instead of 1.
How can I filter rows using vector index instead of column index or name?
Thanks!