To filter a dataframe based on specific column names in R, you can use the dplyr
package. Here's an example code block that filters a dataframe based on the columns "pirate" and "actor":
main.r304 chars10 lines
In the code above, we first load the dplyr
package. We then define an example dataframe called df
, with columns "pirate", "actor", and "food". Finally, we use the %>%
operator to select only the columns "pirate" and "actor" and store the resulting dataframe in filtered_df
.
Note that if you want to keep only the rows that have non-missing values in both columns, you can add the drop_na()
function to the end of the select()
statement, like this:
main.r101 chars3 lines
This will remove any rows that have missing values in either the "pirate" or "actor" columns.
gistlibby LogSnag