当值为 0 时传播收集有问题
Spread gather having issue when values are 0
我有一个 table,我试图使用 tidyr 的 spread gather 来旋转它。下面是下面的数据集
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
我们可以按组排序以避免重复
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
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)
我有一个 table,我试图使用 tidyr 的 spread gather 来旋转它。下面是下面的数据集
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
我们可以按组排序以避免重复
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
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)