我假设您的意思是您实际上想要对某些值求和,并且您的 data.frame 看起来像这样:
mydf <- structure(
list(FIRM = c("FIRM1", "FIRM1", "FIRM1", "FIRM1", "FIRM1", "FIRM2", "FIRM2"),
WORKER = c("A", "A", "A", "B", "B", "A", "C"),
HOURS = c(10L, 20L, 15L, 13L, 12L, 9L, 16L)),
.Names = c("FIRM", "WORKER", "HOURS"),
class = "data.frame", row.names = c(NA, -7L))
mydf
# FIRM WORKER HOURS
# 1 FIRM1 A 10
# 2 FIRM1 A 20
# 3 FIRM1 A 15
# 4 FIRM1 B 13
# 5 FIRM1 B 12
# 6 FIRM2 A 9
# 7 FIRM2 C 16
然后,您可以使用xtabs
:
xtabs(HOURS ~ FIRM + WORKER, mydf)
# WORKER
# FIRM A B C
# FIRM1 45 25 0
# FIRM2 9 0 16
或者,您可以melt
使用您的数据集并对其进行重塑dcast
:
library(reshape2)
dfL <- melt(mydf, id.vars=c("FIRM", "WORKER"))
dcast(dfL, FIRM ~ variable + WORKER, fun.aggregate=sum, value.var="value")
# FIRM HOURS_A HOURS_B HOURS_C
# 1 FIRM1 45 25 0
# 2 FIRM2 9 0 16