我有一个10个客户的交易数据集,从01-01-2013到2016年11月1日。我手动分割每个客户的数据集,如下所示,但我找不到如何创建循环来执行此操作。什么是最好的循环?
customer_1 <- transactions[1:47,]
customer_2 <- transactions[48:94,]
customer_3 <- transactions[95:141,]
customer_4 <- transactions[142:188,]
customer_5 <- transactions[189:235,]
customer_6 <- transactions[236:282,]
customer_7 <- transactions[283:329,]
customer_8 <- transactions[330:376,]
customer_9 <- transactions[377:423,]
customer_10 <- transactions[424:468,]
答案 0 :(得分:0)
您应该使用拆分来拆分数据框:
out <- split( transactions, f = transactions$customer_id)
然后,如果您想按客户分配变量,则可以执行
counter = 1
for (elt in out){
assign(paste("customer", counter, sep ="_"), elt)
counter <- counter + 1
}
哪个会创建变量customer_1,customer_2 ....