3

我的数据框如下所示:

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

我能想到的唯一方法是遍历数据框并手动构建另一个,但似乎必须有更好的方法。

似乎我想要与此处要求的相反将列切换到数据帧中的行

4

4 回答 4

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

奖励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
于 2013-06-04T22:37:38.557 回答
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

文档可以在这里找到。

于 2015-03-13T09:26:31.687 回答
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
于 2013-06-04T21:56:48.653 回答
3

Here's a data.table alternative:

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
于 2013-06-04T23:30:30.650 回答