Search code examples
rsubsetlogical-operators

Subset a dataframe with multipe logical operators R


I have a dataframe that I would like to subset or split into 3 new dataframe based on values in 2 specific columns.

the dataframe data is as follows

structure(list(Analyte = c("Fe", "SiO2", "Al2O3", "Mn", "P"), 
    Units = c("%", "%", "%", "%", "%"), Value = c("65.64", "1.131", 
    "0.845", "0.02", "0.122"), `1 SD` = c("0.218585", "0.037177", 
    "0.022415", "0.002315", "0.001914"), `1 SD within Lab` = c("0.171935", 
    "0.022832", "0.01248", "0.000866", "0.001005"), Interval = c("0.113", 
    "0.023", "0.014", "0.002", "0.001"), `95% Lower CI` = c("65.5275075004927", 
    "1.10799350503001", "0.831047973875986", "0.017751684702168", 
    "0.120781663382338"), `95% Upper CI` = c("65.752959166174", 
    "1.15385316163666", "0.859832026124014", "0.0212631301126469", 
    "0.123288336617662"), `Coeff of Var` = c("0.3", "3.3", "2.7", 
    "11.6", "1.6"), `Number of Labs` = c("10", "10", "10", "9", 
    "10"), `Number of Analysis` = c("60", "60", "60", "54", "60"
    )), row.names = c(NA, 5L), class = "data.frame")

I would like my three tables to be "Certified', 'Provisional' and 'informational'

A Certified table is when the 'Coeff of Var' is >= 10 and Number of labs is >=5 Provisional table is when the 'Coeff of Var' is between 10 and 20 or 'Number of Labs' is <5 and >3 Informational table is essentially everything else but I have made the rule

I have 3 small blocks of code that goes as below but they either do not produce a result or the wrong outcomes

Certified <- subset(df,
                    'Coeff of Var' <= 10 &
                    'Number of Labs' >= 5)

Provisional <- subset(df,
                       'Coeff of Var' < 20 & 
                          'Coeff of Var' > 10 |
                          'Number of Labs' < 5)


Informational <- subset(df,
                        'Coeff of Var' > 20 | 
                        'Number of Labs' < 3)

I have tried using '&&' in place of a '&' but without success. Where have I gone wrong?


Solution

  • All the numbers in your dataframe is of type character. We can use type.convert to change it into their respective types.

    Besides, it is not advised to have column names with spaces in it but if you have that you can refer them with backticks and not quotes. If we refer them with quotes it is comparing the string value with the number. So try :

    df <- type.convert(df, as.is = TRUE)
    
    Certified <- subset(df,`Coeff of Var` <= 10 & `Number of Labs` >= 5)
    Provisional <- subset(df,`Coeff of Var` < 20 & `Coeff of Var` > 10 | `Number of Labs` < 5)
    Informational <- subset(df, `Coeff of Var` > 20 | `Number of Labs` < 3)