I want to add columns to my data frame df depending on the contents of the Column CountryID of the same dataframe. I got it to work but I have 60 of those copy-paste-lines.
df$Country1 <- ifelse(as.integer(df$CountryID == 1), 1, 0)
df$Country2 <- ifelse(as.integer(df$CountryID == 2), 1, 0)
df$Country3 <- ifelse(as.integer(df$CountryID == 3), 1, 0)
... 60 more lines
To make the code a bit more organized I want to write a function createColumns that I just have to call and the entire code within the brackets will be executed. My latest attempt did not create any columns. Does anyone know how to solve this?
createColumns <- function() {
df$Country1 <- ifelse(as.integer(df$CountryID == 1), 1, 0)
df$Country2 <- ifelse(as.integer(df$CountryID == 2), 1, 0)
df$Country3 <- ifelse(as.integer(df$CountryID == 3), 1, 0)
... 60 more lines
return(invisible())
}