How to find the column number of a string column based on a string match in an R data frame?

R ProgrammingServer Side ProgrammingProgramming

A data frame might be very long and contain columns with only string values as well as numerical values. While doing the analysis, we might want to check which columns contain a particular string value. For example, if we have a column with string values as A, B, and C and we want to check which column contains a value “A” then apply function can be used as shown in the below examples.

Example

Consider the below data frame −

 Live Demo

x1<-sample(LETTERS[1:4],20,replace=TRUE)
x2<-rpois(20,2)
df1<-data.frame(x1,x2)
df1

Output

  x1 x2
1  A 3
2  B 1
3  B 2
4  A 2
5  D 3
6  C 2
7  A 3
8  B 1
9  D 3
10 A 1
11 B 0
12 A 2
13 D 2
14 B 0
15 B 3
16 B 1
17 D 4
18 C 1
19 B 4
20 B 1

Checking the column where A exists -

Example

sapply(df1, function(x) any(x=="A"))

Output

 x1    x2
TRUE FALSE

Here, the output for column x1 is TRUE, thus the column x1 contains A.

Example

 Live Demo

y1<-rnorm(20)
y2<-sample(c("Rooh","Frank","Christina"),20,replace=TRUE)
df2<-data.frame(y1,y2)
df2

Output

      y1       y2
1 -1.05746268 Christina
2 -0.46353464 Rooh
3 -0.30686060 Rooh
4 0.22968835 Christina
5 0.36001582 Rooh
6 0.21773469 Christina
7 0.52068600 Christina
8 -0.28930238 Christina
9 2.75670872 Rooh
10 -0.70136439 Frank
11 1.49660551 Frank
12 0.02635582 Rooh
13 -2.20396449 Frank
14 0.82857546 Frank
15 0.36148329 Rooh
16 -0.82422499 Frank
17 -0.50081026 Rooh
18 -0.07842975 Christina
19 -1.19349053 Rooh
20 -0.55590686 Frank

Checking the column where Rooh exists -

Example

sapply(df2, function(x) any(x=="Rooh"))

Output

  y1   y2
FALSE TRUE
raja
Published on 08-Dec-2020 06:31:00
Advertisements