I have trouble generating the following dummy-variables in R:
I'm analyzing yearly time series data (time period 1948-2009). I have two questions:
How do I generate a dummy variable for observation #10, i.e. for year 1957 (value = 1 at 1957 and zero otherwise)?
How do I generate a dummy variable which is zero before 1957 and takes the value 1 from 1957 and onwards to 2009?
The simplest way to produce these dummy variables is something like the following:
More generally, you can use
ifelse
to choose between two values depending on a condition. So if instead of a 0-1 dummy variable, for some reason you wanted to use, say, 4 and 7, you could useifelse(year == 1957, 4, 7)
.The
ifelse
function is best for simple logic like this.Also, if you want it to return character data then you can do so.
Categorical variables with nesting...
This is the most straightforward option.
another way you can do it is use
I read this on the kaggle forum:
Package
mlr
includescreateDummyFeatures
for this purpose:createDummyFeatures
drops original variable. https://www.rdocumentation.org/packages/mlr/versions/2.9/topics/createDummyFeaturesI use such a function (for data.table):
Usage: