dcast unexpected output

I am trying to move this table from long format to wide which only has two columns. And I am receiving this unexpected result.
How to solve it !!!!

df=fread("  variable value
G_EEV1   495
 T_EEV1   496
 LT1    20
G_EEV3   490
 T_EEV3   492
LT3    18
G_EEV4   484
 T_EEV4   484
 LT4    16
G_EEV5   418
T_EEV5   420
LT5     5
G_EEV1   495
T_EEV1   496
LT1    20
G_EEV3   490
T_EEV3   492
LT3    18
G_EEV4   484
T_EEV4   484")
str(df)
dfw<-dcast(df,value~variable)
dfw
> dfw
    value G_EEV1 G_EEV3 G_EEV4 G_EEV5 LT1 LT3 LT4 LT5 T_EEV1 T_EEV3 T_EEV4 T_EEV5
 1:     5      0      0      0      0   0   0   0   1      0      0      0      0
 2:    16      0      0      0      0   0   0   1   0      0      0      0      0
 3:    18      0      0      0      0   0   2   0   0      0      0      0      0
 4:    20      0      0      0      0   2   0   0   0      0      0      0      0
 5:   418      0      0      0      1   0   0   0   0      0      0      0      0
 6:   420      0      0      0      0   0   0   0   0      0      0      0      1
 7:   484      0      0      2      0   0   0   0   0      0      0      2      0
 8:   490      0      2      0      0   0   0   0   0      0      0      0      0
 9:   492      0      0      0      0   0   0   0   0      0      2      0      0
10:   495      2      0      0      0   0   0   0   0      0      0      0      0
11:   496      0      0      0      0   0   0   0   0      2      0      0      0

Gracias

You need to specify what to do with the values, e.g.:

dfw<-dcast(df, value ~ variable, fun.aggregate = sum)

Thanks, it is a simpler solution, than the one I was using:

df[,Smp:=.I]
df
setcolorder(df,c(3,1,2))
dfw<-dcast(df,Smp~variable,fill =0L)
dfw

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.