与tidyr蔓延混淆

时间:2015-03-03 18:00:05

标签: r tidyr spread

我的数据框如下:

ddd <- structure(list(sample_date = structure(c(1400612280, 1400612280, 
1400612280, 1400612280, 1400612280, 1400612280, 1400616420, 1400616420, 
1400616420, 1400616420, 1400616420, 1400616420, 1400604780, 1400604780, 
1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 
1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 
1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 
1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 
1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 
1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780
), class = c("POSIXct", "POSIXt"), tzone = ""), chemical_name = structure(c(1L, 
2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 
6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 
4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 
2L), .Label = c("Conductivity (field)", "Dissolved Oxygen (field)", 
"pH (field)", "Salinity (field)", "Temperature (field)", "Turbidity (field)"
), class = "factor"), result_value = c(29188, 6.42, 7.52, 18.08, 
15.38, 0, 30780, 5.05, 7.42, 19.17, 14.46, 0.5, 28972, 6.64, 
7.47, 17.94, 15.26, -0.1, 29066, 6.42, 7.49, 18, 15.23, 0, 29057, 
6.38, 7.49, 18, 15.24, 0, 29058, 6.35, 7.49, 18, 15.22, 0, 29048, 
6.31, 7.49, 17.99, 15.24, 0, 29043, 6.31, 7.49, 17.99, 15.25, 
0, 29054, 6.3), row = 1:50), .Names = c("sample_date", "chemical_name", 
"result_value", "row"), class = c("tbl_df", "data.frame"), row.names = c(NA, 
-50L))

数据框的头部如下:

           sample_date            chemical_name result_value row
1  2014-05-20 11:58:00     Conductivity (field)     29188.00   1
2  2014-05-20 11:58:00 Dissolved Oxygen (field)         6.42   2
3  2014-05-20 11:58:00               pH (field)         7.52   3
4  2014-05-20 11:58:00         Salinity (field)        18.08   4
5  2014-05-20 11:58:00      Temperature (field)        15.38   5
6  2014-05-20 11:58:00        Turbidity (field)         0.00   6

我想要做的是将此格式转换为宽格式,并尝试在spread中使用tidyr函数。

在使用spread函数之前,我创建了一个名为row的新变量,以便删除duplication error

ddd$row <- 1:nrow(ddd)

现在,我使用了spread函数,如下所示:

ddd1 <- ddd %>% spread(key = chemical_name, result_value)

它似乎有效但不完全符合我的预期。 sample_date是一个POSIXct对象,当它被转换时,hour, minute and second没有显示,因此输出看起来很乱。

sample_date row Conductivity (field) Dissolved Oxygen (field) pH (field) Salinity (field) Temperature (field) Turbidity (field)
1   2014-05-19 121                29670                       NA         NA               NA                  NA                NA
2   2014-05-19 122                   NA                        6         NA               NA                  NA                NA
3   2014-05-19 123                   NA                       NA       7.54               NA                  NA                NA
4   2014-05-19 124                   NA                       NA         NA            18.37                  NA                NA
5   2014-05-19 125                   NA                       NA         NA               NA               14.73                NA
6   2014-05-19 126                   NA                       NA         NA               NA                  NA                 3
7   2014-05-19 127                30042                       NA         NA               NA                  NA                NA
8   2014-05-19 128                   NA                        6         NA               NA                  NA                NA
9   2014-05-19 129                   NA                       NA       7.54               NA                  NA                NA
10  2014-05-19 130                   NA                       NA         NA            18.61                  NA                NA

列名似乎是正确的但不是值。对于每个日期,我都有conductivity, dissolved oxygen, salinity etc..的值。请帮助我解决此问题需要做些什么。

更新:

新数据已更新

ddd <- structure(list(sample_date = structure(c(1400612280, 1400612280, 
    1400612280, 1400612280, 1400612280, 1400612280, 1400616420, 1400616420, 
    1400616420, 1400616420, 1400616420, 1400616420, 1400604780, 1400604780, 
    1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 
    1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 
    1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 
    1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 
    1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 
    1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 
    1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 
    1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 
    1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 
    1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 
    1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 
    1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 
    1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 
    1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 
    1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 
    1400604780, 1400604780, 1400604780, 1400604780, 1400521500, 1400521500, 
    1400521500, 1400521500, 1400521500, 1400521500, 1400526900, 1400526900, 
    1400526900, 1400526900, 1400526900, 1400526900, 1400482800, 1400482800, 
    1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 
    1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 
    1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 
    1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 
    1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 
    1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 
    1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 
    1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 
    1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 
    1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 
    1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 
    1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 
    1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 
    1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 
    1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 
    1400482800, 1400482800, 1400482800, 1400482800, 1400612280, 1400612280, 
    1400612280, 1400612280, 1400612280, 1400612280, 1400616420, 1400616420, 
    1400616420, 1400616420, 1400616420, 1400616420, 1400604780, 1400604780, 
    1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 
    1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 
    1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 
    1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 
    1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 
    1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 
    1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 
    1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 
    1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 
    1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 
    1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 
    1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 
    1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 
    1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 
    1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 1400604780, 
    1400604780, 1400604780, 1400604780, 1400604780, 1400521500, 1400521500, 
    1400521500, 1400521500, 1400521500, 1400521500, 1400526900, 1400526900, 
    1400526900, 1400526900, 1400526900, 1400526900, 1400482800, 1400482800, 
    1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 
    1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 
    1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 
    1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 
    1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 
    1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 
    1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 
    1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 
    1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 
    1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 
    1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 
    1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 
    1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 
    1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 
    1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 1400482800, 
    1400482800, 1400482800, 1400482800, 1400482800, 1407430680, 1407430680, 
    1407430680, 1407430680, 1407430680, 1407430680, 1407433020, 1407433020, 
    1407433020, 1407433020, 1407433020, 1407433020, 1407429900, 1407429900, 
    1407429900, 1407429900, 1407429900, 1407429900, 1407429900, 1407429900, 
    1407429900, 1407429900, 1407429900, 1407429900, 1407429900, 1407429900, 
    1407429900, 1407429900, 1407429900, 1407429900, 1407429900, 1407429900, 
    1407429900, 1407429900, 1407429900, 1407429900, 1407429900, 1407429900, 
    1407429900, 1407429900, 1407429900, 1407429900, 1407429900, 1407429900, 
    1407429900, 1407429900, 1407429900, 1407429900, 1407429900, 1407429900, 
    1407429900, 1407429900, 1407429900, 1407429900, 1407429900, 1407429900, 
    1407429900, 1407429900, 1407429900, 1407429900, 1407429900, 1407429900, 
    1407429900, 1407429900, 1407429900, 1407429900, 1407429900, 1407429900, 
    1407429900, 1407429900, 1407429900, 1407429900, 1407429900, 1407429900, 
    1407429900, 1407429900, 1407429900, 1407429900, 1407429900, 1407429900, 
    1407429900, 1407429900, 1407429900, 1407429900, 1407429900, 1407429900, 
    1407429900, 1407429900, 1407429900, 1407429900, 1407429900, 1407429900, 
    1407429900, 1407429900, 1407429900, 1407429900, 1407429900, 1407429900, 
    1407429900, 1407429900, 1407429900, 1407429900, 1407429900, 1407429900, 
    1407429900, 1407429900, 1407429900, 1407429900, 1407171180, 1407171180, 
    1407171180, 1407171180, 1407171180, 1407171180, 1407176400, 1407176400, 
    1407176400, 1407176400, 1407176400, 1407176400, 1407170040, 1407170040, 
    1407170040, 1407170040, 1407170040, 1407170040, 1407170040, 1407170040, 
    1407170040, 1407170040, 1407170040, 1407170040, 1407170040, 1407170040, 
    1407170040, 1407170040, 1407170040, 1407170040, 1407170040, 1407170040, 
    1407170040, 1407170040, 1407170040, 1407170040, 1407170040, 1407170040, 
    1407170040, 1407170040, 1407170040, 1407170040, 1407170040, 1407170040, 
    1407170040, 1407170040, 1407170040, 1407170040, 1407170040, 1407170040, 
    1407170040, 1407170040, 1407170040, 1407170040, 1407170040, 1407170040, 
    1407170040, 1407170040, 1407170040, 1407170040, 1407170040, 1407170040, 
    1407170040, 1407170040, 1407170040, 1407170040, 1407170040, 1407170040, 
    1407170040, 1407170040, 1407170040, 1407170040, 1407170040, 1407170040, 
    1407170040, 1407170040, 1407170040, 1407170040, 1407170040, 1407170040, 
    1407170040, 1407170040, 1407170040, 1407170040, 1407170040, 1407170040, 
    1407170040, 1407170040, 1407170040, 1407170040, 1407170040, 1407170040, 
    1407170040, 1407170040, 1407170040, 1407170040, 1407170040, 1407170040, 
    1407170040, 1407170040, 1407170040, 1407170040, 1407170040, 1407170040, 
    1407170040, 1407170040, 1407170040, 1407170040, 1407170040, 1407170040, 
    1407170040, 1407170040, 1407170040, 1407170040), tzone = "", class = c("POSIXct", 
    "POSIXt")), chemical_name = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 
    1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 
    5L,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L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 
    1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 
    5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 
    3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 
    1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 
    5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 
    3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 
    1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 
    5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L), .Label = c("Conductivity (field)", 
    "Dissolved Oxygen (field)", "pH (field)", "Salinity (field)", 
    "Temperature (field)", "Turbidity (field)"), class = "factor"), 
        result_value = c(29188, 6.42, 7.52, 18.08, 15.38, 0, 30780, 
        5.05, 7.42, 19.17, 14.46, 0.5, 28972, 6.64, 7.47, 17.94, 
        15.26, -0.1, 29066, 6.42, 7.49, 18, 15.23, 0, 29057, 6.38, 
        7.49, 18, 15.24, 0, 29058, 6.35, 7.49, 18, 15.22, 0, 29048, 
        6.31, 7.49, 17.99, 15.24, 0, 29043, 6.31, 7.49, 17.99, 15.25, 
        0, 29054, 6.3, 7.49, 18, 15.23, -0.1, 29137, 6.31, 7.49, 
        18.07, 15.22, 0, 29273, 6.19, 7.48, 18.15, 15.08, 0.1, 29338, 
        6.2, 7.48, 18.18, 15.01, 0.2, 29465, 6.16, 7.48, 18.3, 14.95, 
        0.3, 30329, 5.48, 7.42, 18.86, 14.57, 0.4, 30955, 4.88, 7.38, 
        19.31, 14.34, 0.7, 31179, 4.68, 7.37, 19.43, 14.27, 0.9, 
        31390, 4.67, 7.37, 19.52, 14.2, 1, 31541, 4.65, 7.37, 19.82, 
        14.15, 1.9, 29630, 6.74, 7.66, 18.37, 14.29, 10.6, 30175, 
        6.62, 7.66, 18.46, 14.11, 11.8, 29670, 6, 7.54, 18.37, 14.73, 
        3, 30042, 6, 7.54, 18.61, 14.56, 4.1, 31403, 6.17, 7.57, 
        19.6, 14.28, 6.2, 31520, 6.25, 7.59, 19.72, 14.23, 6.6, 31915, 
        6.35, 7.61, 19.97, 14.14, 7.2, 32080, 6.41, 7.62, 20.05, 
        14.08, 7.2, 32520, 6.51, 7.64, 20.49, 13.9, 7.6, 32805, 6.58, 
        7.65, 20.55, 13.78, 8.6, 32876, 6.6, 7.65, 20.6, 13.76, 8.9, 
        32907, 6.6, 7.65, 20.61, 13.73, 8.9, 32920, 6.62, 7.65, 20.64, 
        13.72, 10.9, 32980, 6.62, 7.66, 20.65, 13.71, 10.3, 33019, 
        6.61, 7.66, 20.68, 13.7, 10.7, 33088, 6.61, 7.66, 20.71, 
        13.69, 10.1, 33125, 6.61, 7.66, 20.73, 13.68, 9.6, 33099, 
        6.61, 7.66, 20.74, 13.68, 10.3, 29188, 6.42, 7.52, 18.08, 
        15.38, 0, 30780, 5.05, 7.42, 19.17, 14.46, 0.5, 28972, 6.64, 
        7.47, 17.94, 15.26, -0.1, 29066, 6.42, 7.49, 18, 15.23, 0, 
        29057, 6.38, 7.49, 18, 15.24, 0, 29058, 6.35, 7.49, 18, 15.22, 
        0, 29048, 6.31, 7.49, 17.99, 15.24, 0, 29043, 6.31, 7.49, 
        17.99, 15.25, 0, 29054, 6.3, 7.49, 18, 15.23, -0.1, 29137, 
        6.31, 7.49, 18.07, 15.22, 0, 29273, 6.19, 7.48, 18.15, 15.08, 
        0.1, 29338, 6.2, 7.48, 18.18, 15.01, 0.2, 29465, 6.16, 7.48, 
        18.3, 14.95, 0.3, 30329, 5.48, 7.42, 18.86, 14.57, 0.4, 30955, 
        4.88, 7.38, 19.31, 14.34, 0.7, 31179, 4.68, 7.37, 19.43, 
        14.27, 0.9, 31390, 4.67, 7.37, 19.52, 14.2, 1, 31541, 4.65, 
        7.37, 19.82, 14.15, 1.9, 29630, 6.74, 7.66, 18.37, 14.29, 
        10.6, 30175, 6.62, 7.66, 18.46, 14.11, 11.8, 29670, 6, 7.54, 
        18.37, 14.73, 3, 30042, 6, 7.54, 18.61, 14.56, 4.1, 31403, 
        6.17, 7.57, 19.6, 14.28, 6.2, 31520, 6.25, 7.59, 19.72, 14.23, 
        6.6, 31915, 6.35, 7.61, 19.97, 14.14, 7.2, 32080, 6.41, 7.62, 
        20.05, 14.08, 7.2, 32520, 6.51, 7.64, 20.49, 13.9, 7.6, 32805, 
        6.58, 7.65, 20.55, 13.78, 8.6, 32876, 6.6, 7.65, 20.6, 13.76, 
        8.9, 32907, 6.6, 7.65, 20.61, 13.73, 8.9, 32920, 6.62, 7.65, 
        20.64, 13.72, 10.9, 32980, 6.62, 7.66, 20.65, 13.71, 10.3, 
        33019, 6.61, 7.66, 20.68, 13.7, 10.7, 33088, 6.61, 7.66, 
        20.71, 13.69, 10.1, 33125, 6.61, 7.66, 20.73, 13.68, 9.6, 
        33099, 6.61, 7.66, 20.74, 13.68, 10.3, 32261, 7.84, 7.58, 
        20.15, 23.32, 2.2, 32894, 1.66, 7.12, 20.58, 23.09, 2.5, 
        32313, 7.34, 7.52, 20.16, 23.33, 2.1, 32321, 7.33, 7.52, 
        20.19, 23.35, 2.1, 32318, 7.34, 7.51, 20.19, 23.34, 2.1, 
        32341, 7.34, 7.47, 20.2, 23.28, 2.1, 32330, 6.92, 7.48, 20.19, 
        23.31, 2.1, 32338, 6.77, 7.45, 20.2, 23.3, 2.1, 32350, 6.49, 
        7.42, 20.21, 23.29, 2.2, 32378, 6.24, 7.4, 20.23, 23.23, 
        2.2, 32386, 5.87, 7.37, 20.24, 23.23, 2.1, 32395, 5.49, 7.34, 
        20.23, 23.24, 2.2, 32405, 5.28, 7.32, 20.25, 23.23, 2.1, 
        32509, 4.69, 7.27, 20.32, 23.24, 1.8, 32548, 4.69, 7.25, 
        20.35, 23.23, 1.8, 32669, 2.97, 7.19, 20.39, 23.21, 2.9, 
        32712, 2.82, 7.17, 20.46, 23.2, 2.4, 32750, 2.62, 7.16, 20.48, 
        23.18, 2.2, 35655, 6.54, 7.52, 22.49, 23.48, 4.1, 36934, 
        4.4, 7.4, 23.42, 22.44, 6.3, 35424, 6.9, 7.46, 22.3, 23.27, 
        2.2, 35417, 6.89, 7.46, 22.33, 23.27, 2.3, 35459, 6.72, 7.45, 
        22.36, 23.18, 2.3, 35533, 6.4, 7.43, 22.42, 23.05, 2.6, 35555, 
        6.13, 7.42, 22.44, 23, 3.1, 35674, 5.59, 7.38, 22.52, 22.91, 
        3, 35741, 5.19, 7.36, 22.59, 22.87, 2.8, 35959, 4.92, 7.34, 
        22.72, 22.78, 2.9, 36032, 4.72, 7.33, 22.77, 22.74, 3.3, 
        36154, 4.69, 7.33, 22.86, 22.7, 3.3, 36175, 4.62, 7.34, 22.87, 
        22.69, 3.1, 36253, 4.52, 7.34, 22.92, 22.65, 3.5, 36361, 
        4.43, 7.33, 23, 22.61, 3.4, 36476, 4.33, 7.33, 23.07, 22.56, 
        3.7, 36590, 4.24, 7.33, 23.17, 22.51, 3.9, 36661, 4.21, 7.33, 
        23.21, 22.48, 4.3, 36693, 4.14, 7.33, 23.23, 22.46, 4.3), 
        sys_sample_code = structure(c(6L, 6L, 6L, 6L, 6L, 6L, 6L, 
        6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
        6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
        6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
        6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
        6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
        6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
        6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 1L, 1L, 1L, 1L, 
        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 6L, 
        6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
        6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
        6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
        6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
        6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
        6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
        6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
        6L, 6L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        1L, 1L, 1L, 1L, 1L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
        6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
        6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
        6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
        6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
        6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
        6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
        6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        1L, 1L), .Label = c("NC226", "DK011", "NC227", "NC122", "NC208", 
        "NC172", "MC008", "EB010", "EK022", "WE015", "WE016", "GC015", 
        "GC016", "SP015", "SP016", "HB015", "HB016"), class = "factor")), class = c("tbl_df", 
    "tbl", "data.frame"), row.names = c(NA, -654L), .Names = c("sample_date", 
    "chemical_name", "result_value", "sys_sample_code"))

然后使用Akrun推荐的公式:

res <- ddd %>% 
  group_by(chemical_name) %>%
  mutate(indx=1:n()) %>% #create a sequence column for each group

    #   select(-row) %>% #remove the row column before spreading

  spread(chemical_name, result_value)
res

抱歉发布大型数据集。

我得到的外观如下:

   sample_date sys_sample_code indx Conductivity (field) Dissolved Oxygen (field) pH (field) Salinity (field) Temperature (field) Turbidity (field)
1   2014-05-19           NC226   21                29670                     6.00       7.54            18.37               14.73               3.0
2   2014-05-19           NC226   22                30042                     6.00       7.54            18.61               14.56               4.1
3   2014-05-19           NC226   23                31403                     6.17       7.57            19.60               14.28               6.2
4   2014-05-19           NC226   24                31520                     6.25       7.59            19.72               14.23               6.6
5   2014-05-19           NC226   25                31915                     6.35       7.61            19.97               14.14               7.2
6   2014-05-19           NC226   26                32080                     6.41       7.62            20.05               14.08               7.2
7   2014-05-19           NC226   27                32520                     6.51       7.64            20.49               13.90               7.6
8   2014-05-19           NC226   28                32805                     6.58       7.65            20.55               13.78               8.6
9   2014-05-19           NC226   29                32876                     6.60       7.65            20.60               13.76               8.9
10  2014-05-19           NC226   30                32907                     6.60       7.65            20.61               13.73               8.9

2 个答案:

答案 0 :(得分:3)

你可以尝试

library(dplyr)
library(tidyr)
res <- ddd %>% 
          group_by(chemical_name) %>%
          mutate(indx=1:n()) %>% #create a sequence column for each group
          select(-row) %>% #remove the row column before spreading
          spread(chemical_name, result_value) 

更新

基于新数据集

  res1 <- ddd %>%
             group_by(chemical_name) %>% 
             mutate(indx=1:n())  %>% 
             spread(chemical_name, result_value)
  head(as.data.frame(res1),2)
  #          sample_date sys_sample_code indx Conductivity (field)
  #1 2014-05-19 03:00:00           NC226   21                29670
  #2 2014-05-19 03:00:00           NC226   22                30042
  #  Dissolved Oxygen (field) pH (field) Salinity (field) Temperature (field)
  #1                        6       7.54            18.37               14.73
  #2                        6       7.54            18.61               14.56
  #   Turbidity (field)
  #1               3.0
  #2               4.1

或另一种选择是使用data.table

 library(splitstackshape)
 library(data.table)
 res2 <-  dcast.data.table(getanID(as.data.frame(ddd),'chemical_name'), 
   sample_date+sys_sample_code+.id~chemical_name, value.var='result_value')

 head(res2,2)
 #          sample_date sys_sample_code .id Conductivity (field)
 #1: 2014-05-19 03:00:00           NC226  21                29670
 #2: 2014-05-19 03:00:00           NC226  22                30042
 #    Dissolved Oxygen (field) pH (field) Salinity (field) Temperature (field)
 #1:                        6       7.54            18.37               14.73
 #2:                        6       7.54            18.61               14.56
 #    Turbidity (field)
 #1:               3.0
 #2:               4.1

答案 1 :(得分:1)

尝试重塑包。这段代码似乎符合您的要求:

install.packages("reshape")
library("reshape")
DDD2 <- cast(ddd, sample_date~chemical_name, value= "result_value", mean)

希望有所帮助!

总的来说,这是一个很棒的套餐。您可以在此处找到更多相关信息:

http://www.statmethods.net/management/reshape.html

并且,原始论文发表在“统计软件期刊”上:

http://www.jstatsoft.org/v21/i12/paper