不能重塑为reshape()

问题描述 投票:0回答:1

我正在尝试使用reshape()和spread()以及dcast()来编写相同的操作,但我仍然陷入了基本的重塑。

这是数据的头部,但它在前12行上给出了与整个df相同的错误,其中有82个ID(246行)。

head(alcL, n=12)
   ID COA MALE AGE_14   ALCUSE      PEER
1   1   1    0      0 1.732051 1.2649111
2   1   1    0      1 2.000000 1.2649111
3   1   1    0      2 2.000000 1.2649111
4   2   1    1      0 0.000000 0.8944272
5   2   1    1      1 0.000000 0.8944272
6   2   1    1      2 1.000000 0.8944272
7   3   1    1      0 1.000000 0.8944272
8   3   1    1      1 2.000000 0.8944272
9   3   1    1      2 3.316625 0.8944272
10  4   1    1      0 0.000000 1.7888544
11  4   1    1      1 2.000000 1.7888544
12  4   1    1      2 1.732051 1.7888544

       ID            COA              MALE            AGE_14      ALCUSE           PEER       
 Min.   : 1.0   Min.   :0.0000   Min.   :0.0000   Min.   :0   Min.   :0.000   Min.   :0.0000  
 1st Qu.:21.0   1st Qu.:0.0000   1st Qu.:0.0000   1st Qu.:0   1st Qu.:0.000   1st Qu.:0.0000  
 Median :41.5   Median :0.0000   Median :1.0000   Median :1   Median :1.000   Median :0.8944  
 Mean   :41.5   Mean   :0.4512   Mean   :0.5122   Mean   :1   Mean   :0.922   Mean   :1.0176  
 3rd Qu.:62.0   3rd Qu.:1.0000   3rd Qu.:1.0000   3rd Qu.:2   3rd Qu.:1.732   3rd Qu.:1.5492  
 Max.   :82.0   Max.   :1.0000   Max.   :1.0000   Max.   :2   Max.   :3.606   Max.   :2.5298  


alcW <- alcL %>%
  reshape(data =alcL, 
  direction    ="wide",
  timevar      ="AGE_14",
  idvar        ="ID",
  v.names      =c("ALCUSE","PEER")

Error in `[<-.data.frame`(`*tmp*`, , varying[, i], value = c(1.73205077648163,  : 
  duplicate subscripts for columns

我尝试了一些变化,其中我使用了实际年龄(14,15,16)并获得了有关新列中的孔的消息。

使用AGE_14,即0,1,2,我无法通过重复的下标错误

我确认timeVar中没有NA值。没有人有重复的下标:

AGE_14   0   1   2   3   4 Sum
   0    51  16   9   6   0  82
   1    36  25  14   5   2  82
   2    33  18  17  14   0  82
   Sum 120  59  40  25   2 246

我试图指定变化,虽然我预计它会被创建。关于维数不正确我收到了错误。

alcW <- alcL %>%
  reshape(data =alcL, 
  direction    ="wide",
  timevar      ="AGE_14",
  idvar        ="ID",
  v.names      =c("ALCUSE","PEER"),
  varying      =c(paste0("alc",1:3),paste0("peer",1:3)) )
r reshape
1个回答
0
投票

确定,vary必须是一个列表,每个v.name都有一个元素,每个元素都有一组宽名称

alcW <- alcL %>%
  reshape(data =alcL, 
  direction    ="wide",
  timevar      ="AGE_14",
  idvar        ="ID",
  v.names      =c("ALCUSE","PEER"),
  varying      =list(paste0("alc",1:3),paste0("peer",1:3)))
© www.soinside.com 2019 - 2024. All rights reserved.