user1021713
user1021713

Reputation: 2203

Find the index of the column in data frame that contains the string as value

I have data frame like this :

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

I want to find the index of the column of df that has values matching to string "a". i.e. it should give me 1 as result. I tried using which in sapply but its not working. Anybody knows how to do it without a loop ??

Upvotes: 13

Views: 30295

Answers (2)

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193527

Since you mention you were trying to use sapply() but were unsuccessful, here's how you can do it:

> sapply(df, function(x) any(x == "a"))
 col1  col2  col3 
 TRUE FALSE FALSE 
> which(sapply(df, function(x) any(x == "a")))
 col1 
    1

Of course, you can also use the grep()/grepl() approach if you prefer string matching. You can also wrap your which() function with unname() if you want just the column number.

Upvotes: 5

johannes
johannes

Reputation: 14433

Something like this?

 which(apply(df, 2, function(x) any(grepl("a", x))))

The steps are:

  1. With apply go over each column
  2. Search if a is in this column with grepl
  3. Since we get a vector back, use any to get TRUE if any element has been matched to a
  4. Finally check which elements (columns) are TRUE (i.e. contain the searched letter a).

Upvotes: 18

Related Questions