Home > OS >  Circumventing the ifelse() limit to insert 156 unique values to a large-scale dataframe
Circumventing the ifelse() limit to insert 156 unique values to a large-scale dataframe

Time:11-05

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

CodePudding user response:

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>
  • Related