在here之前有关于这个主题的问题,但我仍然在努力传播这个问题。我希望每个state
都有自己的温度值列。
以下是我的数据dput()
。我称之为df
structure(list(date = c("2018-01-21", "2018-01-21", "2018-01-20",
"2018-01-20", "2018-01-19", "2018-01-19", "2018-01-18", "2018-01-18",
"2018-01-17", "2018-01-17", "2018-01-16", "2018-01-16", "2018-01-15",
"2018-01-15", "2018-01-14", "2018-01-14", "2018-01-12", "2018-01-12",
"2018-01-11", "2018-01-11", "2018-01-10", "2018-01-10", "2018-01-09",
"2018-01-09", "2018-01-08", "2018-01-08", "2018-01-07", "2018-01-07",
"2018-01-06", "2018-01-06", "2018-01-05", "2018-01-05", "2018-01-04",
"2018-01-04", "2018-01-03", "2018-01-03", "2018-01-03", "2018-01-03",
"2018-01-02", "2018-01-02"), tmin = c(24, 31, 31, 29, 44, 17,
32, 7, 31, 7, 31, 6, 30, 13, 30, 1, 43, 20, 33, 52, 42, 29, 30,
29, 26, 32, 33, -2, 29, 0, 23, 3, 19, 11, NA, -3, 22, -3, 24,
-4), state = c("UT", "OH", "UT", "OH", "UT", "OH", "UT", "OH",
"UT", "OH", "UT", "OH", "UT", "OH", "UT", "OH", "UT", "OH", "UT",
"OH", "UT", "OH", "UT", "OH", "UT", "OH", "UT", "OH", "UT", "OH",
"UT", "OH", "UT", "OH", "UT", "OH", "UT", "OH", "UT", "OH")), class = "data.frame", row.names = c(NA,
-40L), .Names = c("date", "tmin", "state"))
我运行的代码是
df %>% spread(state,tmin)
我期望给我以下格式
date UT OH
... ... ...
但我收到错误消息
错误:行(36,38),(35,37)的重复标识符
我尝试了一些不同的东西。我试过的一件事是按日期分组。我当时认为同一天的行导致了spread
的问题。我还尝试使用add_rownames()
创建一个新行,然后使用spread(state,tmin)
,但这也无法解决问题。
答案 0 :(得分:9)
为了使spread
按预期工作,结果数据框必须具有唯一标识的行和列。对于您的数据," date" column是传播后唯一的唯一标识符。但是,第36行和第38行是相同的:
date tmin state
36 2018-01-03 -3 OH
38 2018-01-03 -3 OH
这使得tidyr处于尝试将两个数据点解析为同一行和列的不可能的位置。此外,第35行和第37行都具有相同的日期和状态,再次创建了将两个不同值放入新数据框中相同位置的不可能的情况:
date tmin state
35 2018-01-03 NA UT
37 2018-01-03 22 UT
以下数据清理将使传播成为可能:
df %>%
filter(!is.na(tmin)) %>% # remove NA values
unique %>% # remove duplicated rows
spread(state, tmin)
date OH UT
1 2018-01-02 -4 24
2 2018-01-03 -3 22
3 2018-01-04 11 19
4 2018-01-05 3 23
5 2018-01-06 0 29
...