If function with two criteria

Hello,

I'm trying do determine if some countries faced economic crisis in a given period by comparing the GDP value (mean_gdp) to the historical mean and standard deviation (histmeangdp) and (histsdgdp). I have tried to use ifelse() but it doesn't work, as I need to specify the country.
mydata$crisis_gdp <- ifelse(mydata$mean_gdp<(histmeangdp - histsdgdp), 1, 0)

You have a reproducible example below:

The main dataframe (where I want to insert a new column indicating if there was crisis or not)

data.frame(
  stringsAsFactors = FALSE,
              bill = c("Acuerdo contra el tráfico ilícito de migrantes entre los Estados Partes del Mercosur",
                       "Acuerdo contra el tráfico ilícito de migrantes entre los Estados Partes del Mercosur",
                       "Acuerdo contra el tráfico ilícito de migrantes entre los Estados Partes del Mercosur",
                       "Acuerdo contra el tráfico ilícito de migrantes entre los Estados Partes del Mercosur",
                       "Acuerdo contra el tr√ɬ°fico il√ɬ≠cito de migrantes entre los Estados Partes del Mercosur, la Rep√ɬļblica de Bolivia y la Rep√ɬļblica de Chile",
                       "Acuerdo contra el tr√ɬ°fico il√ɬ≠cito de migrantes entre los Estados Partes del Mercosur, la Rep√ɬļblica de Bolivia y la Rep√ɬļblica de Chile",
                       "Acuerdo contra el tr√ɬ°fico il√ɬ≠cito de migrantes entre los Estados Partes del Mercosur, la Rep√ɬļblica de Bolivia y la Rep√ɬļblica de Chile",
                       "Acuerdo contra el tr√ɬ°fico il√ɬ≠cito de migrantes entre los Estados Partes del Mercosur, la Rep√ɬļblica de Bolivia y la Rep√ɬļblica de Chile",
                       "Acuerdo de admisión de títulos y grados universitarios para el ejercicio de actividades académicas en los Estados Parte del Mercosur",
                       "Acuerdo de admisión de títulos y grados universitarios para el ejercicio de actividades académicas en los Estados Parte del Mercosur",
                       "Acuerdo de admisión de títulos y grados universitarios para el ejercicio de actividades académicas en los Estados Parte del Mercosur",
                       "Acuerdo de admisión de títulos y grados universitarios para el ejercicio de actividades académicas en los Estados Parte del Mercosur"),
             cntry = c("Argentina","Brazil",
                       "Paraguay","Uruguay","Argentina","Brazil","Paraguay",
                       "Uruguay","Argentina","Brazil","Paraguay","Uruguay"),
          mean_gdp = c(403612.265657552,
                       2186552.85062478,23721.7319773096,34647.2272436441,403612.265657552,
                       2131515.9358528,23721.7319773096,34647.2272436441,
                       291817.529327736,1561301.28167233,23083.9415915064,
                       44880.0711416869)
)

The dataframe with the GDP values, divided by year and country

data.frame(
  stringsAsFactors = FALSE,
              year = c("1990","1991","1992","1993",
                       "1994","1995","1996","1997","1998","1999","2000",
                       "2001","2002","2003","2004","2005","2006","2007",
                       "2008","2009","2010","2011","2012","2013","2014",
                       "2015","2016","2017","2018"),
         Argentina = c(204022.709576019,
                       225605.750803947,247277.638553345,261428.668260367,276686.170019566,
                       268813.868517326,283670.377245591,306679.014181442,
                       318486.704781525,307704.474172928,305276.689188273,
                       291817.529327736,260025.512893345,283004.073515091,
                       308558.133837269,335870.650498235,362898.670600149,
                       395587.315892942,411637.215422162,387274.363587343,
                       426487.434874632,452093.534425313,447453.153875727,458215.851022343,
                       446702.64932211,458902.812680434,449356.129667934,
                       462021.24554028,450168.774848251),
            Brazil = c(1190328.7904512,
                       1202589.17699285,1196095.19543709,1254943.07905259,1328357.24917717,
                       1384413.92509244,1414185.73384553,1462195.16145589,
                       1467138.81261922,1474004.10627881,1538682.66125963,
                       1560068.7562445,1607704.86065684,1626046.02392186,
                       1719705.69987883,1774772.93573377,1845089.2393129,
                       1957083.76872605,2056781.44097077,2054193.76311241,2208838.10859319,
                       2296626.68016874,2340748.92041541,2411084.2746306,
                       2423235.07223542,2337312.89010631,2260744.4619857,
                       2290651.15086124,2320824.15750549),
          Paraguay = c(15072.4009298585,
                       15444.4660701346,15706.4703187341,16481.7981450403,17358.286599311,
                       18542.6095659408,18834.4303983244,19633.4841408321,
                       19646.8423228185,19378.4507957219,18930.0062043638,
                       18772.11959474,18768.1015361058,19579.0234359506,
                       20373.4263282599,20808.0914770334,21808.3608199,22990.7278981641,
                       24452.736056455,24389.6549826671,27107.573694974,
                       28259.404602775,28107.2240515083,30473.148310721,
                       31954.4146425446,32938.7301255886,34359.3462542358,
                       36061.4300076708,37271.7135902413),
           Uruguay = c(22100.9277328455,
                       22883.0372408505,24698.0255693715,25354.3869873348,27200.5267832485,
                       26806.7722354053,28302.0426702529,29730.6489648803,
                       31074.1443152037,30471.5507314667,29883.4709399851,
                       28734.7114813469,26512.9415151065,26726.4459698004,
                       28063.8801838554,30157.4827229858,31393.5104782487,
                       33447.120372214,35847.3341150742,37368.513657464,
                       40284.5318602359,42364.0729820816,43862.9895982757,45897.1526850981,
                       47383.6656404033,47559.3364376997,48362.9932304934,
                       49616.242186666,50420.0668036221)
)

I appreciate your help in advance!

I don't think that the data you provided is suitable.
Argentina has two 'mean gdp's so what should its gdp for every year be compared to ?

In this case, I'm not comparing years to the historical median (that covers 30 years). "mean_gdp" is the average GDP that the country had during the period on which the bill was in the congress (e.g. 2007-2008, 2009-2011, etc)

I've aggregated everything in the same data frame:

data.frame(
  stringsAsFactors = FALSE,
              bill = c("Acuerdo contra el tráfico ilícito de migrantes entre los Estados Partes del Mercosur",
                       "Acuerdo contra el tráfico ilícito de migrantes entre los Estados Partes del Mercosur",
                       "Acuerdo contra el tráfico ilícito de migrantes entre los Estados Partes del Mercosur",
                       "Acuerdo contra el tráfico ilícito de migrantes entre los Estados Partes del Mercosur",
                       "Acuerdo contra el tr√ɬ°fico il√ɬ≠cito de migrantes entre los Estados Partes del Mercosur, la Rep√ɬļblica de Bolivia y la Rep√ɬļblica de Chile",
                       "Acuerdo contra el tr√ɬ°fico il√ɬ≠cito de migrantes entre los Estados Partes del Mercosur, la Rep√ɬļblica de Bolivia y la Rep√ɬļblica de Chile",
                       "Acuerdo contra el tr√ɬ°fico il√ɬ≠cito de migrantes entre los Estados Partes del Mercosur, la Rep√ɬļblica de Bolivia y la Rep√ɬļblica de Chile",
                       "Acuerdo contra el tr√ɬ°fico il√ɬ≠cito de migrantes entre los Estados Partes del Mercosur, la Rep√ɬļblica de Bolivia y la Rep√ɬļblica de Chile",
                       "Acuerdo de admisión de títulos y grados universitarios para el ejercicio de actividades académicas en los Estados Parte del Mercosur",
                       "Acuerdo de admisión de títulos y grados universitarios para el ejercicio de actividades académicas en los Estados Parte del Mercosur",
                       "Acuerdo de admisión de títulos y grados universitarios para el ejercicio de actividades académicas en los Estados Parte del Mercosur",
                       "Acuerdo de admisión de títulos y grados universitarios para el ejercicio de actividades académicas en los Estados Parte del Mercosur"),
             cntry = c("Argentina","Brazil",
                       "Paraguay","Uruguay","Argentina","Brazil","Paraguay",
                       "Uruguay","Argentina","Brazil","Paraguay","Uruguay"),
          mean_gdp = c(403612.265657552,
                       2186552.85062478,23721.7319773096,34647.2272436441,403612.265657552,
                       2131515.9358528,23721.7319773096,34647.2272436441,
                       291817.529327736,1561301.28167233,23083.9415915064,
                       44880.0711416869),
  histmediangdp_ar = c(318486.704781525,
                       318486.704781525,318486.704781525,318486.704781525,318486.704781525,
                       318486.704781525,318486.704781525,318486.704781525,
                       318486.704781525,318486.704781525,318486.704781525,
                       318486.704781525),
  histmediangdp_br = c(1719705.69987883,
                       1719705.69987883,1719705.69987883,1719705.69987883,1719705.69987883,
                       1719705.69987883,1719705.69987883,1719705.69987883,
                       1719705.69987883,1719705.69987883,1719705.69987883,
                       1719705.69987883),
  histmediangdp_py = c(20373.4263282599,
                       20373.4263282599,20373.4263282599,20373.4263282599,20373.4263282599,
                       20373.4263282599,20373.4263282599,20373.4263282599,
                       20373.4263282599,20373.4263282599,20373.4263282599,
                       20373.4263282599),
  histmediangdp_uy = c(30471.5507314667,
                       30471.5507314667,30471.5507314667,30471.5507314667,30471.5507314667,
                       30471.5507314667,30471.5507314667,30471.5507314667,
                       30471.5507314667,30471.5507314667,30471.5507314667,
                       30471.5507314667),
      histsdgdp_ar = c(83358.3745124158,
                       83358.3745124158,83358.3745124158,83358.3745124158,83358.3745124158,
                       83358.3745124158,83358.3745124158,83358.3745124158,
                       83358.3745124158,83358.3745124158,83358.3745124158,
                       83358.3745124158),
      histsdgdp_br = c(426895.964680008,
                       426895.964680008,426895.964680008,426895.964680008,426895.964680008,
                       426895.964680008,426895.964680008,426895.964680008,
                       426895.964680008,426895.964680008,426895.964680008,
                       426895.964680008),
      histsdgdp_py = c(6570.65438784468,
                       6570.65438784468,6570.65438784468,6570.65438784468,6570.65438784468,
                       6570.65438784468,6570.65438784468,6570.65438784468,
                       6570.65438784468,6570.65438784468,6570.65438784468,
                       6570.65438784468),
      histsdgdp_uy = c(8966.5882894399,
                       8966.5882894399,8966.5882894399,8966.5882894399,8966.5882894399,
                       8966.5882894399,8966.5882894399,8966.5882894399,
                       8966.5882894399,8966.5882894399,8966.5882894399,8966.5882894399)
)

And I'm trying to use this code:

mydata$crisis_gdp <- if(mydata$cntry=="Argentina"){
  ifelse(mydata$mean_gdp<(mydata$histmediangdp_ar - mydata$histsdgdp_ar), "1", "0")
} else if(mydata$cntry=="Brazil"){
  ifelse(mydata$mean_gdp<(mydata$histmediangdp_br - mydata$histsdgdp_br), "1", "0")
} else if(mydata$cntry=="Paraguay"){
  ifelse(mydata$mean_gdp<(mydata$histmediangdp_py - mydata$histsdgdp_py), "1", "0")
} else if(mydata$cntry=="Uruguay"){
  ifelse(mydata$mean_gdp<(mydata$histmediangdp_uy - mydata$histsdgdp_uy), "1", "0")
} else {}

However I don't get the results for each country and receive the following error:

Warning message:
In if (mydata$cntry == "Argentina") { :
the condition has length > 1 and only the first element will be used

Much appreciated nirgrahamuk! It works perfectly well.
I was not familiar with case_when()

One thing to watch out for with case_when() is what happens to entries that don't fit any of your criteria: they will be set to NA unless you specify a final argument TRUE ~ "result". The TRUE syntax is a little confusing, but if you're interested I wrote more about it here.

It looks like Nir's code will work well for your case. But just in case you end up working on a dataset where all the options aren't necessarily covered by Argentina, Brazil, Paraguay, and Uruguay, it can be good to know how to specify the "else" portion of the vectorized "ifelse" (which is basically what case_when is.)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

The outcome that I want is:
"1" if mean_gdp was one standard deviation (histsdgdp_xx) below the median (histmediangdp_xx) and 0 if it was not.

Okay, I should have been clearer. The "xx" symbolises the country code to have a more concise sentence.
So, I rephrase myself: "1" if mean_gdp was one standard deviation (histsdgdp_ar, histsdgdp_br, histsggdp_py or histsdgdp_uy) below the median (histmediangdp_ar, histmediangdp_br, histmediangdp_py, histmediangdp_uy) and 0 if it was not.
The main point is: each row represent one of the four countries and I need to calculate the if statement using the data (median and standard deviation) for the country in each row.

you are describing calculations on variables that you havent provided in your reprex.


mydata$crisis_gdp <- dplyr::case_when(
  mydata$cntry == "Argentina" ~ ifelse(mydata$mean_gdp < (mydata$histmediangdp_ar - mydata$histsdgdp_ar), "1", "0"),
  mydata$cntry == "Brazil" ~ ifelse(mydata$mean_gdp < (mydata$histmediangdp_br - mydata$histsdgdp_br), "1", "0"),
  mydata$cntry == "Paraguay" ~ ifelse(mydata$mean_gdp < (mydata$histmediangdp_py - mydata$histsdgdp_py), "1", "0"),
  mydata$cntry == "Uruguay" ~ ifelse(mydata$mean_gdp < (mydata$histmediangdp_uy - mydata$histsdgdp_uy), "1", "0")
)

#in case you want to understand the results
mydata$crisis_gdp_diff <- dplyr::case_when(
  mydata$cntry == "Argentina" ~ mydata$histmediangdp_ar - mydata$histsdgdp_ar,
  mydata$cntry == "Brazil" ~ mydata$histmediangdp_br - mydata$histsdgdp_br,
  mydata$cntry == "Paraguay" ~ mydata$histmediangdp_py - mydata$histsdgdp_py,
  mydata$cntry == "Uruguay" ~ mydata$histmediangdp_uy - mydata$histsdgdp_uy
)