write a code for selecting only the rows which contain a specific word in one of the coulmns in r

Assuming you have a data frame in R, you can first create a logical vector indicating whether the specific word exists in a certain column using grepl(). Then, you can use this logical vector to subset the data frame using subsetting with square brackets [ ]. Here's an example:

main.r
# Create a sample data frame
df <- data.frame(Name = c("Alice", "Bob", "Charlie"), Age = c(25, 30, 35), Occupation = c("Engineer", "Doctor", "Teacher"))

# Select only the rows where the Occupation column contains the word "Engineer"
df_subset <- df[grepl("Engineer", df$Occupation), ]

# View the subsetted data frame
df_subset
329 chars
9 lines

This will output:

main.r
     Name Age Occupation
1   Alice  25   Engineer
50 chars
3 lines

In this example, the grepl("Engineer", df$Occupation) function creates a logical vector that is TRUE for rows where "Engineer" appears in the Occupation column. The [ ] operator subsets the rows of the data frame using this vector.

gistlibby LogSnag