filter a dataframe by columns name pirate and actor in r

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.r
library(dplyr)

# Example dataframe
df <- data.frame(pirate = c("Jack Sparrow", "Captain Hook"),
                 actor = c("Johnny Depp", "Dustin Hoffman"),
                 food = c("rum", "apples"))

# Filter dataframe based on columns "pirate" and "actor"
filtered_df <- df %>% select(pirate, actor)
304 chars
10 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.r
# Filter and drop rows with missing values
filtered_df <- df %>% select(pirate, actor) %>% drop_na()
101 chars
3 lines

This will remove any rows that have missing values in either the "pirate" or "actor" columns.

gistlibby LogSnag