jeudi 1 octobre 2020

if statements with several conditions and several variations

I have the following data table in R:

       industry fyear change.in.employeegrowth change.in.netincome.to.sales change.in.ROA change.in.Assetturnover change.in.RandD change.in.CAPEX.byassets
1     Agriculture  1999               0.08766928                  0.036667693   0.021561014              0.08213972    1.893469e-03              0.014274404
2     Agriculture  2000               0.13963964                  0.066484354   0.027813095              0.15047066    4.116929e-03              0.028307019
3     Agriculture  2001               0.13636364                  0.041775993   0.030575742              0.05965700    8.678983e-03              0.014702146
4     Agriculture  2002               0.05484111                  0.092764170   0.058518761              0.04699455    2.078513e-03              0.010364144
5     Agriculture  2003               0.08757912                  0.084572235   0.041094305              0.10765641    4.061465e-03              0.008522656
6     Agriculture  2004               0.04970685                  0.058833426   0.028568214              0.02540688    1.275619e-02              0.005628402
7     Agriculture  2005               0.17954545                  0.040047709   0.041380006              0.08353320    3.917954e-02              0.009383056
8     Agriculture  2006               0.19047619                  0.063405763   0.047080200              0.07955826    2.465875e-02              0.005213245
9     Agriculture  2007               0.09165972                  0.098566476   0.064550850              0.09336734    9.296165e-03              0.008958315
10    Agriculture  2008               0.04227658                  0.076702017   0.056679238              0.12820537    2.575690e-03              0.010149566

and 47 more industries.

Now I want to create a dummy variable for certain industries and years, but I am struggling with the formulation of my if statement. The dummy should print 1 if true and nothing if not(alternatively 0). However, I do not want to override previous statements by repeated code.

compustat.medians$industry is of class factor compustat.medians$fyear is of class integer

I have tried the following syntax:

compustat.medians$pre.wave.year <-if( (compustat.medians$industry == "Food Products") & (compustat.medians$fyear == 2012) )
                                 or ( (compustat.medians$industry == "Candy and Soda") & (compustat.medians$fyear == 2012) )
                                 or ( (compustat.medians$industry == "Recreation") & (compustat.medians$fyear == 2005) )
{
  print(1)
}

I get the error: the condition has length > 1 and only the first element will be used for all variations, I try.

Is there a way to use the if statement for several conditions, and with several possible combinations at once?

thanks,

Aucun commentaire:

Enregistrer un commentaire