在reshape2中使用timevar转换数据框,与重塑基函数一样?

jub*_*uba 5 r reshape2

假设我有以下数据框:

d <- data.frame(id=c(1,1,1,2,2,3,3,3), time=c(1,2,3,1,2,1,2,3), var=runif(8))

 d
  id time       var
1  1    1 0.3733586
2  1    2 0.5743769
3  1    3 0.8253280
4  2    1 0.8136957
5  2    2 0.8726963
6  3    1 0.1105549
7  3    2 0.9527002
8  3    3 0.5690021
Run Code Online (Sandbox Code Playgroud)

使用基本reshape功能,我可以通过指定a ìdvar(识别属于同一单元的行)和a timevar(识别同一单元的不同观察值)将其转换为"宽"格式:

reshape(d, idvar="id", timevar="time", direction="wide")

  id     var.1     var.2     var.3
1  1 0.3733586 0.5743769 0.8253280
4  2 0.8136957 0.8726963        NA
6  3 0.1105549 0.9527002 0.5690021
Run Code Online (Sandbox Code Playgroud)

我试图用它的dcast功能来做reshape2,但没找到方法.你知道是否有可能吗?

编辑: Ananda Mahto的评论和回答是完全正确的,真正的问题是当原始数据框有多个var列时.我的例子不合适,抱歉.

A5C*_*2T1 8

以下工作没有?

dcast(d, id ~ time)
# Using var as value column: use value.var to override.
#   id         1          2         3
# 1  1 0.2869739 0.59591690 0.8989719
# 2  2 0.4533770 0.14741778        NA
# 3  3 0.1286770 0.02465634 0.7363114

## OR, to get rid of the message:
## dcast(d, id ~ time, value.var = "var")
Run Code Online (Sandbox Code Playgroud)

不过,我怀疑你问的是一个不同的问题(如我的评论所述).特别是,如果您从以下开始:

set.seed(1)
d <- data.frame(id = c(1,1,1,2,2,3,3,3), 
                time = c(1,2,3,1,2,1,2,3), 
                var1 = runif(8),
                var2 = runif(8))
Run Code Online (Sandbox Code Playgroud)

使用基数R reshape,它只是一行:

reshape(d, direction = "wide", idvar = "id", timevar = "time")
#   id    var1.1    var2.1    var1.2     var2.2    var1.3    var2.3
# 1  1 0.2655087 0.6291140 0.3721239 0.06178627 0.5728534 0.2059746
# 4  2 0.9082078 0.1765568 0.2016819 0.68702285        NA        NA
# 6  3 0.8983897 0.3841037 0.9446753 0.76984142 0.6607978 0.4976992
Run Code Online (Sandbox Code Playgroud)

让我们dcast从"reshape2" 尝试相同的方法.这是我们可能想要采取的方法:

library(reshape2)
dcast(d, id ~ time)
# Using var2 as value column: use value.var to override.
#   id         1          2         3
# 1  1 0.6291140 0.06178627 0.2059746
# 2  2 0.1765568 0.68702285        NA
# 3  3 0.3841037 0.76984142 0.4976992
Run Code Online (Sandbox Code Playgroud)

但这不起作用,因为dcast期待单一value.var.所以,我们需要melt再次提供数据.

d2 <- melt(d, id.vars = c("id", "time"))
head(d2)
#   id time variable     value
# 1  1    1     var1 0.2655087
# 2  1    2     var1 0.3721239
# 3  1    3     var1 0.5728534
# 4  2    1     var1 0.9082078
# 5  2    2     var1 0.2016819
# 6  3    1     var1 0.8983897
Run Code Online (Sandbox Code Playgroud)

现在,我们可以dcast很容易地使用.

dcast(d2, id ~ variable + time)
#   id    var1_1    var1_2    var1_3    var2_1     var2_2    var2_3
# 1  1 0.2655087 0.3721239 0.5728534 0.6291140 0.06178627 0.2059746
# 2  2 0.9082078 0.2016819        NA 0.1765568 0.68702285        NA
# 3  3 0.8983897 0.9446753 0.6607978 0.3841037 0.76984142 0.4976992
Run Code Online (Sandbox Code Playgroud)