Check for Column Name in R


As a basic in R, today we will see whether a particular column exists in a data frame.
Suppose we have a data frame named ‘TestData’ having 3 columns; A, B, C
Capture
Now, run the following and store value in a variable named, bool_val
1
> bool_val <- "C" %in% colnames(TestData)
(where C is the name of column and TestData is the data frame)
If value in bool_val is ‘TRUE’ then the column exists, and if it’s ‘FALSE’ then the column doesn’t exists.

Comments