Locate column and row labels in an R data frame using a condition

The row.names and colnames functions in R can be used to find the names of columns and rows in a data frame based on a condition. When you run all the code snippets provided in the previous section as a single program, the resulting output will be displayed. If you want to locate the columns that contain the value 92 in the created data frame above, simply add the code snippet shown below. The output will be displayed in the console. If you run all the code snippets provided in the previous section as one program, you will see the resulting output. One user asked how to retrieve column names in R, but this task is not well-documented.


In order to locate the
column names
and
row names
within an R
data frame
, row.names and
colnames function
can be utilized based on a specified condition. These functions allow for the definition of the condition for identifying both row and column names, as demonstrated in the following examples.

Example 1

The code below generates an example data frame.

x1<-rpois(20,1)
x2<-rpois(20,8)
x3<-rpois(20,20)
df1<-data.frame(x1,x2,x3)
df1

The following dataframe is created

  x1 x2 x3
1  1 11 15
2  3  6 15
3  0  9 18
4  2 10 26
5  1  9 17
6  1  7 23
7  0 11 21
8  2 11 23
9  1  6 22
10 2  6 28
11 2  7 22
12 0  8 16
13 1  7 28
14 1  7 25
15 0  6 14
16 0 10 23
17 0 12 16
18 0  8 23
19 2  8 17
20 2  8 21

To identify rows containing the value 17 in
created data frame
, simply augment the aforementioned snippet with the subsequent code.

x1<-rpois(20,1)
x2<-rpois(20,8)
x3<-rpois(20,20)
df1<-data.frame(x1,x2,x3)
row.names(df1[which(df1==17,arr.ind=T)[,1],] )

Output

Upon running the program that comprises of all the provided code snippets, it produces the resultant output.

[1] "5" "19"

The above snippet can be analyzed to identify the columns that contain the value 17 by using the following code with reference to
data frame, add
.

x1<-rpois(20,1)
x2<-rpois(20,8)
x3<-rpois(20,20)
df1<-data.frame(x1,x2,x3)
colnames(df1)[apply(df1, 2, function(x) any(x==17))]

Output

Upon running the combined code consisting of the aforementioned snippets, the resulting output is as follows –

[1] "x3"

Example 2

The code below generates a sample data frame.

y1<-sample(1:100,20)
y2<-sample(1:100,20)
y3<-sample(1:100,20)
df2<-data.frame(y1,y2,y3)
df2

The following dataframe is created

   y1 y2 y3
1  75 52 32
2  37  5 46
3  43 31 60
4 100 30 11
5  28 18 79
6  31 80 53
7   8 85 49
8  62 38 56
9  48 82 15
10 97 48 69
11 69 25 40
12 12 92 21
13 77 55 26
14 39 95 63
15 82 61 75
16 98 40 14
17 61 78 22
18 93 63 58
19 10 21 17
20 2 68 83

To determine the rows containing the value 92 in the previously generated data frame, simply include the subsequent code in the aforementioned snippet.

y1<-sample(1:100,20)
y2<-sample(1:100,20)
y3<-sample(1:100,20)
df2<-data.frame(y1,y2,y3)
row.names(df2[which(df2==92,arr.ind=T)[,1],] )

Output

Upon executing the aforementioned code snippets collectively, the resulting output is produced.

[1] "12"

To identify the columns that contain the value 92 in the data frame created above, simply include the following code snippet.

y1<-sample(1:100,20)
y2<-sample(1:100,20)
y3<-sample(1:100,20)
df2<-data.frame(y1,y2,y3)
colnames(df2)[apply(df2, 2, function(x) any(x==92))]

Output

Upon running the program that combines the provided code snippets, the resulting output is as follows.

[1] "y2"

Frequently Asked Questions

Posted in Uncategorized