为什么围绕我的浮点数?

时间:2017-10-14 02:09:44

标签: r dcast

我有一个数据框:

> 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

当我使用dcast函数转换它时,值列不保留为浮点数:

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

为什么" area_class"这是一个数字类,当我转换它时不保留为浮点数?

1 个答案:

答案 0 :(得分:3)

OP帖子中的输出可能是因为重复的行使dcast fun.aggregate作为默认选项length。为了防止这种情况,要么创建序列列,要么聚合value.var(取决于要求)

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