I have data frame looking like this:
我有这样的数据帧:
category fan_id likes
A 10702397 1
B 10702397 4
A 35003154 1
B 35003154 1
C 35003154 2
I'd like to convert it into a following data frame
我想把它转换成如下的数据帧
fan_id A B C
10702397 1 4 0
35003154 1 1 2
The only way that I can think of is looping over the data frame and constructing another manually but it seems like there has to be a better way.
我能想到的唯一方法是在数据框架上循环并手工构建另一个,但似乎必须有更好的方法。
It seems like I want to the opposite of what was asked here Switch column to row in a data.frame
似乎我想要与这里要求的相反,在data.frame中从一列切换到另一列
4 个解决方案
#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
4
Base reshape
function method:
基础重塑函数法:
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
method:
奖金xtabs方法:
result2 <- as.data.frame.matrix(xtabs(likes ~ fan_id + category, data=dat))
A B C
10702397 1 4 0
35003154 1 1 2
Exact format with a fix:
精确格式与一个固定:
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
#3
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
#4
2
The shortest solution I could think of only consists of typing a one-letter method.
我所能想到的最简短的解决方案只有一个字母的方法。
The transpose function t
also works on dataframes, not just on matrices.
转置函数t也适用于dataframes,而不仅仅适用于矩阵。
Example
> 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
Documentation can be found here.
文档可以在这里找到。
#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
4
Base reshape
function method:
基础重塑函数法:
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
method:
奖金xtabs方法:
result2 <- as.data.frame.matrix(xtabs(likes ~ fan_id + category, data=dat))
A B C
10702397 1 4 0
35003154 1 1 2
Exact format with a fix:
精确格式与一个固定:
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
#3
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
#4
2
The shortest solution I could think of only consists of typing a one-letter method.
我所能想到的最简短的解决方案只有一个字母的方法。
The transpose function t
also works on dataframes, not just on matrices.
转置函数t也适用于dataframes,而不仅仅适用于矩阵。
Example
> 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
Documentation can be found here.
文档可以在这里找到。