我的数据框看起来像这样:
category fan_id likes
A 10702397 1
B 10702397 4
A 35003154 1
B 35003154 1
C 35003154 2
我想将其转换为以下数据框
fan_id A B C
10702397 1 4 0
35003154 1 1 2
我能想到的唯一方法是循环数据框并手动构建另一个,但似乎必须有更好的方法。
所要求的相反答案 0 :(得分:4)
基础reshape
函数方法:
dat <- data.frame(
category=c("A","B","A","B","C"),
fan_id=c(10702397,10702397,35003154,35003154,35003154),
likes=c(1,4,1,1,2)
)
result <- reshape(dat,idvar="fan_id",timevar="category",direction="wide")
names(result)[2:4] <- gsub("^.*\\.","",names(result)[2:4])
result
fan_id A B C
1 10702397 1 4 NA
3 35003154 1 1 2
Bonus xtabs
方法:
result2 <- as.data.frame.matrix(xtabs(likes ~ fan_id + category, data=dat))
A B C
10702397 1 4 0
35003154 1 1 2
带有修复的确切格式:
data.frame(fan_id=rownames(result2),result2,row.names=NULL)
fan_id A B C
1 10702397 1 4 0
2 35003154 1 1 2
答案 1 :(得分:3)
> library(reshape2)
> dat <- data.frame(category=c("A","B","A","B","C"),fan_id=c(10702397,10702397,35003154,35003154,35003154),likes=c(1,4,1,1,2))
> dcast(dat,fan_id~category,fill=0)
Using likes as value column: use value.var to override.
fan_id A B C
1 10702397 1 4 0
2 35003154 1 1 2
答案 2 :(得分:3)
这是data.table
替代方案:
require(data.table)
dt <- as.data.table(df) # where df is your original data.frame
out <- dt[CJ(unique(fan_id), unique(category))][,
setattr(as.list(likes), 'names', as.character(category)),
by = fan_id][is.na(C), C := 0L]
# fan_id A B C
# 1: 10702397 1 4 0
# 2: 35003154 1 1 2
答案 3 :(得分:3)
我能想到的最短解决方案只包括输入单字母方法。
转置函数t
也适用于数据框,而不仅仅适用于矩阵。
> data = data.frame(a = c(1,2,3), b = c(4,5,6))
> data
a b
1 1 4
2 2 5
3 3 6
> t(data)
[,1] [,2] [,3]
a 1 2 3
b 4 5 6
可以找到文档here。