dimanche 10 juillet 2016

If Else statement with multiple conditions in R

I need to create a new data frame using if else condition in R. I am not sure if my logic used in the code is correct or not. I have created two vectors (a,b) to find if my two data frames (data_model1, data_model2) exist.

My requirements:

  1. If both data frames exists, then combine both (cbind) and create new data frame
  2. If a=TRUE (exist) and b=FALSE (do not exist), then create new data frame of values of data_model1.
  3. If a=FALSE (do not exist) and b=TRUE (exist), then create new data frame of values of data_model2.

Below is my code:

a <- exists("data_model1") && is.data.frame(get("data_model1"))
b <- exists("data_model2") && is.data.frame(get("data_model2"))

if ( a== "TRUE" && b== "TRUE")
{ 
  data_model <- cbind(data_model1, data_model2)
} 
else if ( a== "TRUE" && b== "FALSE" ) 
{
  data_model <- data_model1
} 
else 
{
  data_model <- data_model2
}

The error which I am getting is:

Error: unexpected 'else' in "else"

Can anyone please let me know how I can resolve this? Thank You!

Aucun commentaire:

Enregistrer un commentaire