我是reshape2
和data.table
的新手,并尝试学习语法。
我有一个data.table
,我希望从每个分组变量的多行转换为每个分组变量一行。为简单起见,我们让它成为一个客户表,其中一些客户共享地址。
library(data.table)
# Input table:
cust <- data.table(name=c("Betty","Joe","Frank","Wendy","Sally"),
address=c(rep("123 Sunny Rd",2),
rep("456 Cloudy Ln",2),
"789 Windy Dr"))
我希望输出具有以下格式:
# Desired output looks like this:
(out <- data.table(address=c("123 Sunny Rd","456 Cloudy Ln","789 Windy Dr"),
cust_1=c("Betty","Frank","Sally"),
cust_2=c("Joe","Wendy",NA)) )
# address cust_1 cust_2
# 1: 123 Sunny Rd Betty Joe
# 2: 456 Cloudy Ln Frank Wendy
# 3: 789 Windy Dr Sally NA
我想要cust_1 ... cust_n的列,其中n是每个地址的最大客户数。我并不关心订单 - Joe是否是cust_1而Betty是cust_2,反之亦然。
答案 0 :(得分:12)
Just pushed a commit到data.table v1.9.5
。 dcast
现在
value.var
列和多个fun.aggregate
函数有了这个,我们可以做到:
dcast(cust, address ~ paste0("cust", cust[, seq_len(.N),
by=address]$V1), value.var="name")
# address cust1 cust2
# 1: 123 Sunny Rd Betty Joe
# 2: 456 Cloudy Ln Frank Wendy
# 3: 789 Windy Dr Sally NA
答案 1 :(得分:4)
# My attempt:
setkey(cust,address)
x <- cust[,list(name, addr_cust_num=rank(name,ties.method="random")), by=address])
x[,addr_cust_num:=paste0("cust_",addr_cust_num)]
y <- dcast.data.table(x, address ~ addr_cust_num, value.var="name")
y
请注意,我必须paste0
&#34; cust _&#34;字首。在我添加该步骤之前,我使用的是setnames(y, names(y), sub("(\\d+)","cust_\\1",names(y)) )
,这似乎是一种笨重(但可能更快)的解决方案。
想知道是否有更好的方法来做前缀。
或者,您可以直接通过引用将列添加到cust
:
# no need to set key
cust[, cust := paste("cust", seq_len(.N), sep="_"), by=address]
dcast.data.table(cust, address ~ cust, value.var="name")
# address cust_1 cust_2
# 1: 123 Sunny Rd Betty Joe
# 2: 456 Cloudy Ln Frank Wendy
# 3: 789 Windy Dr Sally NA