0

there is a dataframe with two column as below,and i want to change it into a dataframe with 3 column

df <- data.frame(key=c('a','a','a','b','b'),value=c(1,2,2,1,3))

I have tried it in python,that's ok,but in r i have no idea

the expect output should be like

  1 2 3
a 1 2 0
b 1 0 1
Cui Weijie
  • 11
  • 2

1 Answers1

0
library(data.table)
dcast(key~value, data=df, fun.aggregate=length)
#   key 1 2 3
# 1   a 1 2 0
# 2   b 1 0 1
r2evans
  • 141,215
  • 6
  • 77
  • 149