目前我已经能够获得我的数据:
date category revenue cost
20150100 1 2670935 1290161.8
20150200 1 2487379 1201022.2
20150300 1 2663918 1244556.4
20150400 1 2557039 1208289.3
20150500 1 2372128 1026402.6
20150600 1 2981372 919436.4
20150100 2 2670935 1290161.8
20150200 2 2487379 1201022.2
20150300 2 2663918 1244556.4
20150400 2 2557039 1208289.3
20150500 2 2372128 1026402.6
20150600 2 2981372 919436.4
我想将其重塑到以下所示的位置(使用额外的cat2列):
date category cat1.revenue cat1.cost
20150100 1 2670935 1290161.8
20150200 1 2487379 1201022.2
20150300 1 2663918 1244556.4
20150400 1 2557039 1208289.3
20150500 1 2372128 1026402.6
20150600 1 2981372 919436.4
我不知道如何使用dplyr或其他数据争用程序包来使我的数据看起来像这样。
答案 0 :(得分:0)
一种可能的解决方案可能是使用gather
,unite
和spread
作为
library(tidyverse)
df %>% gather(key, value, -date, -category)
%>% unite(combined, key, category, sep = "_cat") %>%
spread(combined, value)
# date cost_cat1 cost_cat2 revenue_cat1 revenue_cat2
#1 20150100 1290161.8 1290161.8 2670935 2670935
#2 20150200 1201022.2 1201022.2 2487379 2487379
#3 20150300 1244556.4 1244556.4 2663918 2663918
#4 20150400 1208289.3 1208289.3 2557039 2557039
#5 20150500 1026402.6 1026402.6 2372128 2372128
#6 20150600 919436.4 919436.4 2981372 2981372
数据:强>
df <- read.table( text = "date category revenue cost
20150100 1 2670935 1290161.8
20150200 1 2487379 1201022.2
20150300 1 2663918 1244556.4
20150400 1 2557039 1208289.3
20150500 1 2372128 1026402.6
20150600 1 2981372 919436.4
20150100 2 2670935 1290161.8
20150200 2 2487379 1201022.2
20150300 2 2663918 1244556.4
20150400 2 2557039 1208289.3
20150500 2 2372128 1026402.6
20150600 2 2981372 919436.4",
header = T, stringsAsFactor = F)