filter a dataframe from a specific value in a column in r

To filter a dataframe from a specific value in a column in R, you can use the subset() function or the dplyr package's filter() function.

Here are two examples.

Using subset():

main.r
# create a sample dataframe
df <- data.frame(name = c("Alice", "Bob", "Charlie", "David"), age = c(25, 30, 35, 40))

# subset the dataframe to only include rows where the age column has a value of 30
subset(df, age == 30)
222 chars
6 lines

Using dplyr's filter() function:

main.r
library(dplyr)

# create a sample dataframe
df <- data.frame(name = c("Alice", "Bob", "Charlie", "David"), age = c(25, 30, 35, 40))

# filter the dataframe to only include rows where the age column has a value of 30
df %>% filter(age == 30)
241 chars
8 lines

Both of these examples will return a dataframe that only includes rows where the age column has a value of 30.

gistlibby LogSnag