数据框中的数据透视列

时间:2015-01-31 00:26:59

标签: r

我的数据框如下:

data<-data.frame(names= c("Bob","Bob", "Fred","Fred","Tom"), id =c(1,1,2,2,3),amount = c(100,200,400,500,700), status = c("Active","Not Active","Active","Retired","Active"))
data

 names id amount     status
1   Bob  1    100     Active
2   Bob  1    200 Not Active
3  Fred  2    400     Active
4  Fred  2    500    Retired
5   Tom  3    700     Active

我想透视“状态”列,以便“金额”数据显示在新状态列下,以便结果如下所示:

names     id    Active    Not Active  Retired
Bob       1      100         200
Fred      2      400                   500
Tom       3      700

这可能吗?什么是最好的方式?

4 个答案:

答案 0 :(得分:2)

我现在不得不将评论转化为答案。这是Hadleyverse的版本:

library(tidyr)
spread(data, status, amount)

##   names id Active Not Active Retired
## 1   Bob  1    100        200      NA
## 2  Fred  2    400         NA     500
## 3   Tom  3    700         NA      NA

答案 1 :(得分:1)

以下是使用dcast包中的reshape2的解决方案:

library(reshape2)

dcast(data, names + id ~ status, value.var="amount")

#   names id Active Not Active Retired
# 1   Bob  1    100        200      NA
# 2  Fred  2    400         NA     500
# 3   Tom  3    700         NA      NA

答案 2 :(得分:1)

这将是基本方法:

> xtabs(amount~names+status, data=data)
      status
names  Active Not Active Retired
  Bob     100        200       0
  Fred    400          0     500
  Tom     700          0       0

答案 3 :(得分:0)

这是另一个base R选项

 reshape(data, idvar=c('names', 'id'), timevar='status', direction='wide')
 #  names id amount.Active amount.Not Active amount.Retired
 #1   Bob  1           100               200             NA
 #3  Fred  2           400                NA            500
 #5   Tom  3           700                NA             NA