One-hot encoding in R- creating dataframe column names from variables in a loop : <p>I am using a dataframe called "rawData" which has a column called "Season" with values ranging from 1 to 4.
I am trying to use a loop to perform one-hot-encoding, i.e create 4 new columns called "Season 1" , "Season 2", "Season 3", "Season 4", where each column has a binary indicator value of 1/0 showing whether the Season in the column name is occurring for each data-point. So far I have tried this:</p>

<pre><code>for (i in 1:4){
text&lt;-paste("Season", toString(i), sep = " ")
if (rawData$season==i) {
rawData$text&lt;-1
}
}
</code></pre>

<p>However, I am just getting an additional column in my dataframe called "text" with all values =1. I understand why R is doing this, but I can not figure out an alternative way to make it do what I want. I tried changing the if-then statement to change "rawData$text" to "rawData$paste("Season", toString(i), sep = " ")&lt;-1" but that is giving me an error</p>
