2

我有一个看起来像这样的数据框:

data.frame(name=c("a","a","b","b","c"),
value=c("2011-01-23","2011-01-23","2011-01-23","2011-01-22","2011-01-22"))

name     value
 a        2011-01-23
 a        2011-01-23
 b        2011-01-23
 b        2011-01-22
 c        2011-01-22

我只有 3 个不同的名字,我想把它变成:

a    b   c   date
2    1   0    2011-01-23
0    1   1    2011-01-22

我查看了 reshape 包,但我无法很好地理解文档来解决这个问题。

4

2 回答 2

3

假设您data.framedat,并使用dcast()from reshape2

dcast(dat, value ~ name)

#        value a b c
# 1 2011-01-22 0 1 1
# 2 2011-01-23 2 1 0
于 2013-06-12T05:32:49.363 回答
0

tablet(table(dat))应该这样做(如果您的源对象名为“dat”)。将整个语句包装起来as.data.frame.matrix(.)以获得data.frame.

t(table(dat))
#             name
# value        a b c
#   2011-01-22 0 1 1
#   2011-01-23 2 1 0
as.data.frame.matrix(t(table(dat)))
#            a b c
# 2011-01-22 0 1 1
# 2011-01-23 2 1 0
于 2013-06-12T05:36:20.960 回答