考虑这个令牌数据集:
Data = structure(list(txs = c(-50, -750, -35, -5.96, -61.5, -42.07,
-142.4, -500, 132, -154.89, -109.51, -2000, -50, -40, -24.98,
-15.6, -50, -147.72, -20, -6.6, -5, -20, -13.48, -7.25, -54.09,
-200, -124.11, -30, -50, -30, 400, -10, -0.95, -4.1, -10000,
30, -1.99, 74.03, -6.95, -2.96, -29, -403.6, -6, -6, 5250, -513.57,
-300, -10, -500, -20, -6.45, -7.26, -40, -50, -13.14, 321.29,
-18, 100, -5.5, -25, -59.2, -10.75, -3.2, 270, 65.8, -11.6, -104.78,
-99.39, 0.1, -50, -80, -50, -371.44, -78, 270, -6.3, 40, -2.5,
-29.99, -189.48, -400, -0.29, -20, -6.55, -987.37, -1400, -0.49,
-20, -29.04, -65, -40, -27.5, -17.37, -10, -1092.84, -5.5, -69.93,
-15.07, -400, -4.8), week = structure(c(1439157600, 1454281200,
1471212000, 1445205600, 1448233200, 1451862000, 1449442800, 1453676400,
1460325600, 1460930400, 1445205600, 1454281200, 1460930400, 1444600800,
1462140000, 1471816800, 1443996000, 1448838000, 1479682800, 1453071600,
1447023600, 1473631200, 1465768800, 1433109600, 1445205600, 1433714400,
1466978400, 1441576800, 1459116000, 1451862000, 1436133600, 1440367200,
1456095600, 1458514800, 1456700400, 1450047600, 1440972000, 1446418800,
1465164000, 1441576800, 1442181600, 1453071600, 1461535200, 1460930400,
1438552800, 1464559200, 1447628400, 1434924000, 1437343200, 1436738400,
1443391200, 1438552800, 1440972000, 1446418800, 1446418800, 1453071600,
1453071600, 1457305200, 1444600800, 1462140000, 1435528800, 1457305200,
1437948000, 1440972000, 1437948000, 1433109600, 1461535200, 1453676400,
1454886000, 1454281200, 1441576800, 1441576800, 1471212000, 1453071600,
1451862000, 1442786400, 1443391200, 1439762400, 1436133600, 1461535200,
1442181600, 1468188000, 1442181600, 1453676400, 1466373600, 1443391200,
1450652400, 1454886000, 1439157600, 1441576800, 1463954400, 1442181600,
1446418800, 1454886000, 1476050400, 1461535200, 1456700400, 1456700400,
1435528800, 1456700400), class = c("POSIXct", "POSIXt"), tzone = ""),
num_c = c(1219, 1257, 1195, 33, 1105, 1223, 1257, 1317, 486,
1227, 477, 1039, 1238, 1008, 1137, 1294, 1070, 596, 1295,
1354, 1010, 1294, 1348, 1254, 19, 1185, 24, 1287, 1198, 955,
1324, 1293, 1343, 1162, 1272, 972, 972, 179, 1343, 1105,
1085, 1020, 947, 1375, 1005, 477, 596, 1198, 928, 1137, 1263,
1237, 1054, 1288, 1185, 1115, 1257, 1301, 1294, 1185, 1039,
957, 1131, 33, 477, 1258, 477, 1039, 1362, 1246, 596, 1010,
972, 1238, 477, 1296, 972, 1148, 1105, 24, 553, 1297, 1288,
1223, 789, 1298, 1082, 1353, 1030, 1287, 1203, 1008, 1294,
1227, 1298, 1203, 1346, 1010, 19, 1303)), .Names = c("txs",
"week", "num_c"), row.names = c(NA, -100L), class = "data.frame")
它有三列:
一个名为num_c
:这是一个客户编号
一个名为week
的人:这是周的星期一的日期
num_c
下了订单。
现在,对于每个客户端,我都想使用dplyr来扩展(加入)这个数据集 这样我就可以在每个星期的范围内获得一行 客户已下订单。
此外, new 空单元格(对于txs)应填充NA。 对应于原始(未扩展)数据集中存在的那些周/客户端的txs值应保留其原始值;
我试过了:
library(dplyr)
stretch_Data = Data %>%
group_by(num_c) %>%
right_join(seq(min(week), max(week), by = 'week'), by = "week")
但我得到了;
Error in seq(min(week), max(week), by = "week") : object 'week' not found
这很愚蠢,因为数据确实包含一周的列(非常感谢你)。
我做错了什么?
感谢@ mt1022的解决方案(下面的评论)。这很安静聪明。但仍然存在一个问题:是否可以包含数据中的所有列? - 为了表达我的观点,我添加了一个:txs--那么缺失的值(那些对应于没有购买的周数的那些)应该由NA填充(就像在联合中一样)。与我们购买的客户*周日期相对应的单元格应保留其原始值。
本质上在new(扩展表)上执行na.omit()应返回原始表;就像在关节上一样;
答案 0 :(得分:1)
使用@ mt1002中的解决方案,只需一个简单的添加即可获得txs
列。我已经sum(txs)
获得了一个值,根据您的需要,这也可以是min(txs)
或max(txs)
的单个值。
Data %>% group_by(num_c) %>%
summarise(week = list(seq(min(week), max(week), by = 'week')),
txs = sum(txs)) %>%
unnest(week)
澄清之后,这是我提出的解决方案,除了每周每位用户的订单数量之外,没有订单的数周有NA值。您也可以使用左连接(包括上述查询到df),按num_c
的订单加入周列表。
library(lubridate)
a <- data.frame(week = rep(seq(1,52,1)))
Data %>%
group_by(num_c) %>%
mutate(week_num = week(week)) %>%
group_by(num_c, week_num) %>%
summarise(txs = sum(txs),
number_orders = n()) %>%
full_join(a, by = c("week_num"="week")) %>%
ungroup() %>%
arrange(week_num)