1

My dataframe contains 784,081 firms * 13 years = 10,193,053 unique firm_year observations (e.g. firm1_2007, firm1_2008,firm1_2009, ......, ......, firm784,081 2018, firm784,081 2019 ) from 12 home countries.

Now, I want to insert a column "gdpgrowthpercapita". I have 13 * 12=156 unique values (decimals, e.g. 3.34587) depending on year and homecountry to insert.

  • First, I tried to nest ifelse() statements. This works in general, however, there is a limit of 50 nested statements; everything >50 does not work (see here: Is there a limit for the possible number of nested ifelse statements)

  • Next, I attempted to code "blocks" of ifelse() statemens for every country; however, the problem is that the no argument in ifelse(test, yes, no) cannot be neglected, or - to my knowledge - set in a way that it does not touch the fields where test does not apply.

How can I code my variable?

I am thankful for help! Hopefully, there is an obvious way I missed - as an R Rookie.

Thank you in advance!

Best, Mo

mm_hoewa
  • 13
  • 3
  • 3
    Provide a small, reproducible example with a small data set, for example, 20 - 30 rows, 3 columns (year, firm, country), and 5 unique values. That will be enough to illustrate and test alternate approaches. – dcarlson Nov 01 '21 at 20:34
  • 1
    Have you considered using `merge` or `dplyr::left_join` rather than providing each value specifically? Sounds like you could create a small df with columns year and homecountry then join `by=c("year", "homecountry")` – Dubukay Nov 01 '21 at 21:05
  • left_join worked! I concatinated homecountry and year, and used this column as by= argument. Thank you! – mm_hoewa Nov 03 '21 at 11:19

1 Answers1

0

You shouldn't need to nest the ifelse statements - just define the column ahead of time as NA and then fill it in repeatedly. For the no argument, you can supply the column itself and it'll fill in the values intelligently:

df <- head(data.frame(letters, LETTERS))

df$newcol <- NA
df$newcol <- ifelse(test = is.na(df$newcol) & df$letters=="a", 
                    yes = "this is an a", 
                    no = df$newcol)
df$newcol <- ifelse(test = is.na(df$newcol) & df$letters=="b", 
                    yes = "this is a b", 
                    no = df$newcol)
> df
  letters LETTERS       newcol
1       a       A this is an a
2       b       B  this is a b
3       c       C         <NA>
4       d       D         <NA>
5       e       E         <NA>
6       f       F         <NA>
Dubukay
  • 1,764
  • 1
  • 8
  • 13
  • This worked! Thanks a lot! In the end, I went with left_join. This was easier, as it only took 5-10ish lines of code, whereas with ifelse, I would've had to enter the 156 unique gdpgrpc values for the respective country_year combinations manually. Thank you for your quick and effective help! – mm_hoewa Nov 03 '21 at 11:22
  • Sure! If you liked the answer, feel free to upvote and hit the green check so that others know it's helpful too. – Dubukay Nov 03 '21 at 17:06
  • Yes. Hit the green check. Upvoting not possible yet (as a rookie), but I´ll get there. ;-) – mm_hoewa Nov 04 '21 at 20:00