mardi 19 février 2019

R: Multiple conditions with non existing columns

I am using a program, which provides an excel table as a research result. There are a lot of columns, like Title 1, Title 2 so on.

Sometimes, some of those columns might be absent - like there will be no Title 2 at all.

I want to create code, which will account for tables that might be missing. Basically to 1 check which columns exist in the table, and then filter available data.

The problem is, if while filter, there is a non-existing column - code stops with an error, and I need it to just move on to the next calculation.

I came up with a long solution:

if( "Title 2"%in% (colnames(Meta_Data))&"Meta Description 2"%in%(colnames(Meta_Data))&"H1-2"%in%(colnames(Meta_Data)))

  {Correct <- Meta_Data %>%  
      filter(  `Title 1 Length` > 60 | `Title 1 Length` < 50 
            |  `Title 2 Length` > 60 | `Title 2 Length` < 50
            |  `Meta Description 1 Length`> 300 | `Meta Description 1` < 50
            |  `Meta Description 2 Length`> 300 | `Meta Description 2` < 50
            |  `H1-1`>70   | `H1-1` < 20
            |  `H1-2` > 70 | `H1-2` < 20
            |  `H2-1`>70   | `H2-1` < 20
            |  `H2-2` > 70 | `H2-2` < 20
            )
} else if("Meta Description 2"%in%(colnames(Meta_Data)) & "H1-2"%in%(colnames(Meta_Data)))
  {Correct <- Meta_Data %>%  
      filter   (  `Title 1 Length` > 60 | `Title 1 Length` < 50 
               |  `Meta Description 1 Length`> 300 | `Meta Description 1` < 50
               |  `Meta Description 2 Length`> 300 | `Meta Description 2` < 50
               |  `H1-1`>70   | `H1-1` < 20
               |  `H1-2` > 70 | `H1-2` < 20
               |  `H2-1`>70   | `H2-1` < 20
               |  `H2-2` > 70 | `H2-2` < 20
      )

} else if ("Title 2" %in% (colnames(Meta_Data)) &"H1-2"%in% (colnames(Meta_Data)))
  {Correct <- Meta_Data %>%  
    filter   (  `Title 1 Length` > 60 | `Title 1 Length` < 50 
             |  `Title 2 Length` > 60 | `Title 2 Length` < 50
             |  `Meta Description 1 Length`> 300 | `Meta Description 1` < 50
             |  `H1-1`>70   | `H1-1` < 20
             |  `H1-2` > 70 | `H1-2` < 20
             |  `H2-1`>70   | `H2-1` < 20
             |  `H2-2` > 70 | `H2-2` < 20
    )

} else if("Title 2"%in%(colnames(Meta_Data))&"Meta Description 2"%in%(colnames(Meta_Data)))
  {Correct <- Meta_Data %>%  
    filter   (  `Title 1 Length` > 60 | `Title 1 Length` < 50 
             |  `Title 2 Length` > 60 | `Title 2 Length` < 50
             |  `Meta Description 1 Length`> 300 | `Meta Description 1` < 50
             |  `Meta Description 2 Length`> 300 | `Meta Description 2` < 50
             |  `H1-1`>70   | `H1-1` < 20
             |  `H2-1`>70   | `H2-1` < 20
             |  `H2-2` > 70 | `H2-2` < 20
    )

} else if ("H1-2"%in%(colnames(Meta_Data)))
  { Correct <- Meta_Data %>%  
    filter(  `Title 1 Length` > 60 | `Title 1 Length` < 50 
             |  `Meta Description 1 Length`> 300 | `Meta Description 1` < 50
             |  `H1-1`>70   | `H1-1` < 20
             |  `H1-2` > 70 | `H1-2` < 20
             |  `H2-1`>70   | `H2-1` < 20
             |  `H2-2` > 70 | `H2-2` < 20
    )

} else if ("Meta Description 2"%in%(colnames(Meta_Data)))
  {Correct <- Meta_Data %>%  
    filter(  `Title 1 Length` > 60 | `Title 1 Length` < 50 
             |  `Meta Description 1 Length`> 300 | `Meta Description 1` < 50
             |  `Meta Description 2 Length`> 300 | `Meta Description 2` < 50
             |  `H1-1`>70   | `H1-1` < 20
             |  `H2-1`>70   | `H2-1` < 20
             |  `H2-2` > 70 | `H2-2` < 20
    )

} else if ("Title 2"%in%(colnames(Meta_Data)))
  {Correct <- Meta_Data %>%  
    filter(  `Title 1 Length` > 60 | `Title 1 Length` < 50 
             |  `Title 2 Length` > 60 | `Title 2 Length` < 50
             |  `Meta Description 1 Length`> 300 | `Meta Description 1` < 50
             |  `H1-1`>70   | `H1-1` < 20
             |  `H2-1`>70   | `H2-1` < 20
             |  `H2-2` > 70 | `H2-2` < 20
    )
} else 
  {Correct <- Meta_Data %>%
    filter (`Title 1 Length` > 60 | `Title 1 Length` < 50 
            |  `Meta Description 1 Length`> 300 | `Meta Description 1` < 50
            |  `H1-1`>70   | `H1-1` < 20
            |  `H2-1`>70   | `H2-1` < 20
            |  `H2-2` > 70 | `H2-2` < 20

    )

}

This takes into account Title 2| Meta Description 2 | H1-2

However, it appears I should also account for H2-2 and this will add a lot more iterations/lines of code.

(NOTE: if the column Title 2 exists, then Title 2 Length exists as well, this works with other columns too).

I want to understand, if there is simpler solution (there should be) as itll be a nightmare if there are more than 5 conditions (columns) which might or migt not exist.
(Unfortunately, I dont have good math or coding background)

Aucun commentaire:

Enregistrer un commentaire