Hi I have the below dataframe. Since the column contains NA's the datatype of the column is character. Now, I need to get the column name and index which only contains String value.
In the example below, I want to get the column name and column index of Zo-A and Zo-B:
ZONE-1 Zo-A Zone-3 Zo-B
58 On 75 NA
60 NA NA High
NA Off 68 Low
70 On NA NA
So far I tried to first convert all of them to numeric, which created NA's for Zo-A and Zo-B column. And if I use the below code for column index, I'm getting NA's as a result
a <- which(colnames(df)=="Zo-A" )
integer(0)
match_col <- match(c("Zo-A","Zo-B")names(df))
NA NA
I need to perform below operations:
- I need to first get the
column nameswhich consists ofStringvalues - I need the
column indexfor the same
For what I understand of your question, what you want or need is really, really simple.
First, read the data in.
Now, question (1), "first get the column names which consists of String values". All column names consist of string values so this can be done either with
namesor withcolnames.Now question (2), to get the column index of "the same". (I assume it's of column
Zo-Ayou are asking for.)Data in dput format.
Edit
If you need to get only the column names composed of alphabetic characters and punctuation marks, you can use the following regular expression.