To filter rows in a dataframe based on multiple values from a specific column, you can use the %in%
operator in combination with the filter()
function from the dplyr
package in R.
Here's an example code snippet:
main.r312 chars16 lines
In this example, we first create a sample dataframe df
with two columns: group
and value
. Then, we define the selected_groups
vector to contain the values we want to filter the dataframe by (in this case, "a" and "c"). Next, we use the filter()
function with the %in%
operator to select rows where the value in the group
column matches any of the values in selected_groups
. Finally, we store the filtered dataframe in a new object called filtered_df
and print it to the console.
This will output:
main.r98 chars8 lines
gistlibby LogSnag