我的数据如下:
# A tibble: 6 x 4
name val time x1
<chr> <dbl> <date> <dbl>
1 C Farolillo 7 2016-04-20 51.5
2 C Farolillo 3 2016-04-21 56.3
3 C Farolillo 7 2016-04-22 56.3
4 C Farolillo 13 2016-04-23 57.9
5 C Farolillo 7 2016-04-24 58.7
6 C Farolillo 9 2016-04-25 59.0
我正在尝试使用pivot_wider
函数来扩展基于name
列的数据。我使用以下代码:
yy <- d %>%
pivot_wider(.,names_from = name,values_from = val)
哪个会给我以下警告消息:
Warning message:
Values in `val` are not uniquely identified; output will contain list-cols.
* Use `values_fn = list(val = list)` to suppress this warning.
* Use `values_fn = list(val = length)` to identify where the duplicates arise
* Use `values_fn = list(val = summary_fun)` to summarise duplicates
输出如下:
time x1 out1 out2
2016-04-20 51.50000 <dbl> <dbl>
2 2016-04-21 56.34615 <dbl> <dbl>
3 2016-04-22 56.30000 <dbl> <dbl>
4 2016-04-23 57.85714 <dbl> <dbl>
5 2016-04-24 58.70968 <dbl> <dbl>
6 2016-04-25 58.96774 <dbl> <dbl>
我知道here提到了该问题,并建议使用摘要统计信息来解决该问题。但是我有时间序列数据,因此不想使用汇总统计信息,因为每天都有一个值(而不是多个值)。
我知道问题是因为val
列中有重复项(即在上面的示例中7次出现了3次。
关于如何pivot_wider和克服此问题的任何建议?
数据:
d <- structure(list(name = c("C Farolillo","C Farolillo","Plaza Eliptica","Plaza Eliptica"),val = c(7,3,7,13,9,20,19,4,5,2,6,16,10,11,8,14,32,25,31,34,26,33,35,43,22,21,48,47,27,23,17,24,28,25),time = structure(c(16911,16912,16913,16914,16915,16916,16917,16918,16919,16920,16921,16922,16923,16924,16925,16926,16927,16928,16929,16930,16931,16932,16933,16934,16935,16936,16937,16938,16939,16940,16941,16942,16943,16944,16945,16946,16947,16948,16949,16950,16951,16952,16953,16954,16955,16956,16957,16958,16959,16960,16911,16960),class = "Date"),x1 = c(51.5,56.3461538461538,56.3,57.8571428571429,58.7096774193548,58.9677419354839,64.4615384615385,61.9310344827586,60.3214285714286,59.4137931034483,59.5806451612903,57.3448275862069,64.0333333333333,70.15625,71.3636363636364,62.8125,56.4375,56.4516129032258,51.741935483871,52.84375,53.09375,52.969696969697,54,54.3870967741936,60.3870967741936,64.4516129032258,66.2903225806452,68.2333333333333,69.7741935483871,70.5806451612903,73.8275862068966,72.8181818181818,64.6764705882353,64.4838709677419,68.7741935483871,62.1764705882353,68.969696969697,70.1935483870968,59.6774193548387,59.9677419354839,63.125,67.5882352941177,71.4705882352941,73.8529411764706,76.1935483870968,72.6451612903226,76.0645161290323,76.4193548387097,81.7741935483871,85.0645161290323,51.5,85.0645161290323)),class = c("tbl_df","tbl","data.frame"),row.names = c(NA,-102L))