R 值为0时排列聚集有问题
我有一张桌子,我试图用tidyr的spread gather旋转它。下面是以下数据集R 值为0时排列聚集有问题,r,dplyr,tidy,R,Dplyr,Tidy,我有一张桌子,我试图用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), pl
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"))
)
我们可以一组一组地进行排序,以避免重复
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
库(dplyr)
图书馆(tidyr)
实际%>%
分组人(平台)%>%
变异(rn=行数())%>%
解组%>%
排列(平台、转换)
#或者更广泛地使用pivot
#pivot\u更宽(名称\u from=平台,值\u from=转换)
#一个tibble:12x5
#枣苹果香蕉橙
#
# 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不适用
#6 2020-01-10 60北美
#7 2020-01-10 7不适用
#8 2020-01-10 80北美
#9 2020-01-10 9不适用
#10 2020-01-10 10不适用
#11 2020-01-10 11不适用
#12 2020-01-10不适用
库(datapasta)
图书馆(dplyr)
图书馆(tidyr)
dpasta(图表和数据)
实际百分比
分组人(平台)%>%
变异(rn=行数())%>%
解组%>%
排列(平台、转换)
# %>%
#pivot\u更宽(名称\u from=平台,值\u from=转换)
在您的输入dput中,15行上有一个1表示“香蕉”,而不是“橙子”。我得到以下错误:错误:行号()应仅在数据类型中调用context@SNT你能检查一下你的dplyr
和tidyr
版本吗dplyr:
0.8.3.9000tidyr:
1。00@SNT我也有相同的版本packageVersion('dplyr')[1]'0.8.99.9000'>packageVersion('tidyr')[1]'1.0.0'
。您是否将此应用于相同的数据,即dput数据?我已在下面的回答中发布了该代码。你能运行它吗?是的,它对我很好。dpasta
在代码中做什么
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)