rdataframeimportsumr-colnames

How to make conditional statement based on column titles in R


I am exporting a dataframe to R I want to set a condition so that if the column header matches any of the following name below

Data_design <- c( "age", "sex", "font")

Then an error is flagged in the condition if statement.

For instance, if I import the following table (busa) with the column header ( age, sex, location, geography)

The condition would give an error because the columns do not have the required column header which is age sex and font.

Please can anyone help me with this? The code I have been able to do is below

if (sum(str_sort(colnames(busa)) == str_sort(Data_design)) <length(Data_design)){
print("Failed import structure test")
break
}

But I am getting error.


Solution

  • If understand, you want a test that only passes if all the data frame column names are in your Data_design - is that right?

    If so you can check if the column names are in your list as follows:

    names(busa) %in% Data_design
    
    [1]  TRUE  TRUE FALSE FALSE
    

    To distil that to a single test:

    all(names(busa) %in% Data_design)
    [1] FALSE