r - Find the index of the row in data frame that contain one element in a string vector -


if have data.frame this

df <- data.frame(col1 = c(letters[1:4],"a"),col2 = 1:5,col3 = letters[10:14])  df    col1 col2 col3 1       1    j 2    b    2    k 3    c    3    l 4    d    4    m 5       5    n 

i want row indices contains 1 of element in c("a", "k", "n"); in example, result should 1, 2, 5.

if have large data frame , wish check columns, try this

x <- c("a", "k", "n")  reduce(union, lapply(x, function(a) which(rowsums(df == a) > 0))) # [1] 1 5 2 

and of course can sort end result.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -