Search code examples
rdcast

Why dcast round my float numbers?


I have one data frame as:

> head(df)
     nome   Class_Name area_class       date
1 Arataca agropecuaria    25.7751 1973-07-26
2 Arataca         agua     0.2918 1973-07-26
3 Arataca       bosque     0.0207 1973-07-26
4 Arataca       bosque     3.9335 1973-07-26
5 Arataca         mata    69.9513 1973-07-26
6 Arataca solo exposto     0.0276 1973-07-26

When I converted it with dcast function the value column not keep as float number:

dcast(df, nome+date~Class_Name, value.var = "area_class")
            nome       date agropecuaria agua bosque mata solo exposto
1              Arataca 1973-07-26            1    1      2    1            1
2            Buerarema 1973-07-26            1    0      2    1            1
3              Camacan 1973-07-26            1    0      1    1            0
4               Camamu 1973-07-26            1    1      2    1            1
5            Igrapiúna 1973-07-26            1    1      2    1            1
6               Ilhéus 1973-07-26            1    1      2    1            1
7              Itabuna 1973-07-26            1    0      2    1            1
8              Itacaré 1973-07-26            1    1      2    1            1
9              Ituberá 1973-07-26            1    1      2    1            1
10               Maraú 1973-07-26            1    1      2    1            1
11        Nilo Peçanha 1973-07-26            1    0      2    1            0
12 São José da Vitória 1973-07-26            1    1      2    1            0
13                 Una 1973-07-26            1    1      2    1            1
14             Uruçuca 1973-07-26            1    1      2    1            1

Why "area_class" which is a numeric class not keep as float number when I converted it?


Solution

  • The output in OP's post could be because of the duplicate rows which makes the dcast fun.aggregate to do the length as the default option. Inorder to prevent this, either create a sequence column or aggregate the value.var (depends on the requirement)

    dcast(df, nome+date~Class_Name, value.var = "area_class", fun.aggregate = mean)