df <- data.frame(cod = c(1,1,2,1,2,2),
nom = c("banana", "banana", "orange", "banana", "orange", "orange"),
val = rnorm(6, 0.06, 0.01))
df <- as.data.table(df)
categol = function(base, col){
colss = c(paste(col,'_1',sep=''),paste(col,'_2',sep=''))
base[, colss[1] := 0]
base[, colss[2] := 0]
base[as.name(col) == 1, colss[1] := 1]
base[as.name(col) == 2, colss[2] := 1]
}
categol(df, 'cod')
Unfortunately, this didn't work.