-1

original table is like this:

id food
1 fish
2 egg
2 apple

for each id, should have 1 or 0 value of its food, so the table should look like this:

id food fish egg apple
1 fish 1 0 0
2 egg 0 1 0
2 apple 0 0 1
  • 1
    see answer of Mouad_Seridi here: https://stackoverflow.com/questions/52539750/r-how-to-one-hot-encoding-a-single-column-while-keep-other-columns-still In your case: df %>% mutate(value = 1) %>% spread(food, value, fill = 0) – maarvd Jan 29 '21 at 15:48

1 Answers1

1

A proposition using the dcast() function of the reshape2 package :

df1 <- read.table(header = TRUE, text = "
id  food
1   fish
2   egg
2   apple
")

###

df2 <- reshape2::dcast(data = df1, 
                       formula = id+food ~ food, 
                       fun.aggregate = length, 
                       value.var = "food")
df2
#>   id  food apple egg fish
#> 1  1  fish     0   0    1
#> 2  2 apple     1   0    0
#> 3  2   egg     0   1    0

###

df3 <- reshape2::dcast(data = df1, 
                       formula = id+factor(food, levels=unique(food)) ~ 
                                   factor(food, levels=unique(food)), 
                       fun.aggregate = length, 
                       value.var = "food")
names(df3) <- c("id", "food", "fish", "egg", "apple")
df3
#>   id  food fish egg apple
#> 1  1  fish    1   0     0
#> 2  2   egg    0   1     0
#> 3  2 apple    0   0     1

# Created on 2021-01-29 by the reprex package (v0.3.0.9001)

Regards,

barboulotte
  • 395
  • 2
  • 8