return the row containing a specific value of two different columns in r

You can use the subset function in R to return the row containing a specific value in two different columns of a data frame. Here's an example:

# Sample data frame
df <- data.frame(A = c(1, 2, 3, 4, 5),
                 B = c("apple", "banana", "cherry", "banana", "apple"),
                 C = c(10, 20, 30, 40, 50))

# Value to search for in column A
value_A <- 3

# Value to search for in column B
value_B <- "cherry"

# Return the row containing value_A in column A and value_B in column B
result <- subset(df, A == value_A & B == value_B)

# Print the result
print(result)
435 chars
17 lines

In this example, subset is used to filter the rows in the data frame df where column A is 3 and column B is "cherry". The & operator is used to specify that both conditions should be met.

Make sure to adjust the column names, data values, and comparison operators (==, >, <, etc.) according to your specific use case.

related categories

gistlibby LogSnag