Let's say I have one dataframe that represents a subset brief. Let's call this dataframe brief. Here, the values are the subset rules.
library(tidyverse)
brief <- data.frame(apple = 1, orange = 2, pear = 3)
In a second dataframe, called types, this is the data I'd like to subset.
types <- data.frame(apple_cake = rnorm(5,0,1),
apple_pie = rnorm(5,0, 1),
apple_ice = rnorm(5,0, 1),
orange_cake = rnorm(5,0,1),
orange_pie = rnorm(5, 0, 1),
orange_ice = rnorm(5,0, 1),
pear_cake = rnorm(5,0,1),
pear_pie = rnorm(5, 0, 1),
pear_ice = rnorm(5,0, 1)
)
In my example, I'd like to subset specific apple, orange, and pear food items using dataframe one against dataframe two. I'd like to use the values from the first dataframe brief to subset the columns from the second dataframe types.
I would like to end up with a final dataframe as follows:
final <- types %>%
select(apple_cake, orange_pie, pear_ice)
In the final solution, apple item 1, orange item 2, and pear item 3 remain.
I've tried various dplyr functions but to no avail. I have also looked for similar solutions but do not think they address my problem because these examples might have similar column names across two dataframes.