当值为0时展开收集时有问题

问题描述 投票:1回答:2

我有一张桌子,我试图用提迪尔的撒布收集架来转动它。这是以下数据集

library(datapasta)
dpasta(chart_data)
actual<-data.frame(stringsAsFactors=FALSE,
   conversions = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L,
                   0L, 0L, 0L, 0L),
      platform = c("apple", "apple", "apple", "apple", "apple",
                   "apple", "apple", "apple", "apple", "apple",
                   "apple", "apple", "banana", "banana",
                   "banana", "oranges", "oranges",
                   "oranges", "oranges"),
          date = as.factor(c("2020-01-10", "2020-01-10", "2020-01-10",
                             "2020-01-10", "2020-01-10", "2020-01-10",
                             "2020-01-10", "2020-01-10", "2020-01-10", "2020-01-10",
                             "2020-01-10", "2020-01-10", "2020-01-10", "2020-01-10",
                             "2020-01-10", "2020-01-10", "2020-01-10",
                             "2020-01-10", "2020-01-10"))
)

下面是我用来更改它以扩展聚集的代码

 chart_data <- chart_data %>% 
   tidyr::spread(key = platform, value = conversions)

我想获得的输出是这样的


whatitshouldbe<-data.frame(stringsAsFactors=FALSE,date = as.factor(c("2020-01-10")),
                   apple = c(0L),
                   banana = c(0L),
                   oranges = c(1L)

)

但是当我运行代码时,出现以下错误

Keys are shared for 19 rows:
* 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
* 13, 14, 15
* 16, 17, 18, 19```

How can I fix this or use some other method to convert it. Thank you
r dplyr tidy
2个回答
1
投票

为了避免重复,我们可以按组进行排序

library(dplyr)
library(tidyr)
actual %>% 
   group_by(platform) %>%
   mutate(rn = row_number()) %>%
   ungroup %>% 
   spread(platform, conversions)
   #or use pivot_wider
   # pivot_wider(names_from = platform, values_from = conversions)
# A tibble: 12 x 5
#   date          rn apple banana oranges
#   <fct>      <int> <int>  <int>   <int>
# 1 2020-01-10     1     0      0       0
# 2 2020-01-10     2     0      0       0
# 3 2020-01-10     3     0      1       0
# 4 2020-01-10     4     0     NA       0
# 5 2020-01-10     5     0     NA      NA
# 6 2020-01-10     6     0     NA      NA
# 7 2020-01-10     7     0     NA      NA
# 8 2020-01-10     8     0     NA      NA
# 9 2020-01-10     9     0     NA      NA
#10 2020-01-10    10     0     NA      NA
#11 2020-01-10    11     0     NA      NA
#12 2020-01-10    12     0     NA      NA

0
投票
library(datapasta)
library(dplyr)
library(tidyr)
dpasta(chart_data)
actual<-data.frame(stringsAsFactors=FALSE,
                   conversions = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L,
                                   0L, 0L, 0L, 0L),
                   platform = c("apple", "apple", "apple", "apple", "apple",
                                "apple", "apple", "apple", "apple", "apple",
                                "apple", "apple", "banana", "banana",
                                "banana", "oranges", "oranges",
                                "oranges", "oranges"),
                   date = as.factor(c("2020-01-10", "2020-01-10", "2020-01-10",
                                      "2020-01-10", "2020-01-10", "2020-01-10",
                                      "2020-01-10", "2020-01-10", "2020-01-10", "2020-01-10",
                                      "2020-01-10", "2020-01-10", "2020-01-10", "2020-01-10",
                                      "2020-01-10", "2020-01-10", "2020-01-10",
                                      "2020-01-10", "2020-01-10"))
)





actual %>% 
  group_by(platform) %>%
  mutate(rn = row_number()) %>%
  ungroup %>% 
  spread(platform, conversions)
# %>% 
#   pivot_wider(names_from = platform, values_from = conversions)
© www.soinside.com 2019 - 2024. All rights reserved.