【问题标题】:How do I use the reshape() function more than once successfully in R? [duplicate]如何在 R 中多次成功使用 reshape() 函数? [复制]
【发布时间】:2017-10-06 14:19:41
【问题描述】:

这是我的数据框:

   ID Group x1  x2  x3  y1  y2  y3  z1  z2  z3
    144 1   566 613 597 563 549 562 599 82  469
    167 2   697 638 756 682 695 693 718 82  439.5
    247 4   643 698 730 669 656 669 698 82  514.5
    317 4   633 646 641 520 543 586 559 82  405.5
    344 3   651 678 708 589 608 615 667 82  514
    352 2   578 702 671 536 594 579 591 82  467.5
    382 1   678 690 693 555 565 534 521 82  457.5
    447 3   668 672 718 663 689 751 784 82  506.5
    464 2   760 704 763 514 554 520 564 82  486
    628 1   762 789 783 618 610 645 625 82  536

我有几个宽幅重复的小节,我想将它们改造成长幅。我不确定如何一次重塑所有三个 (x,y,z) 重复变量,所以我选择一个接一个地尝试。所以我可以成功地重塑变量 x:

reshaped.df <- reshape(df, 
                       idvar="ID", 
                       varying= c("x.1", "x.2", "x.3"),
                       timevar="Timex",
                       v.names= "X", 
                       times=c("Part1", "Part2", "Part3"),
                       direction="long") 

当我尝试在新的重塑数据帧上使用相同的重塑方法来融化下一个变量时,它不再起作用了。 所以我尝试运行这个:

reshaped.df <- reshape(reshaped.df, 
                       idvar="ID", 
                       varying= list( c("y.1", "y.2", "y.3")),
                       timevar="Timey",
                       v.names= "Y", 
                       times=c("P1", "P2", "P3"),
                       direction="long") 

我收到以下错误和警告消息:

Error in `row.names<-.data.frame`(`*tmp*`, value = paste(d[, idvar], times[1L],  : 
  duplicate 'row.names' are not allowed
In addition: Warning message:
non-unique values when setting 'row.names': ‘144.Part1’, ‘167.Part1’, ‘247.Part1’, ‘317.Part1’, ‘344.Part1’, ‘352.Part1’, ‘382.Part1’,  ... <truncated>

还有其他方法可以有效地做到这一点吗?

【问题讨论】:

  • 您没有尝试类似melt 的具体原因是什么?即:melt(df, id.vars = c("ID","Group"))
  • 嗨,迈克,如果我尝试简单地melt,我所有的 x、y 和 z 值都在一个巨大的列中。我至少想为 x 保留一列,为 y 保留一列,为 z 保留一列...您如何使用简单的melt 函数指定这一点?
  • 看我的回答,我会使用 reshape

标签: r reshape anova


【解决方案1】:

你可以试试data.table::melt,可以同时融化三个测量组:

library(data.table)
df <- fread('ID Group x1  x2  x3  y1  y2  y3  z1  z2  z3
    144 1   566 613 597 563 549 562 599 82  469
    167 2   697 638 756 682 695 693 718 82  439.5
    247 4   643 698 730 669 656 669 698 82  514.5
    317 4   633 646 641 520 543 586 559 82  405.5
    344 3   651 678 708 589 608 615 667 82  514
    352 2   578 702 671 536 594 579 591 82  467.5
    382 1   678 690 693 555 565 534 521 82  457.5
    447 3   668 672 718 663 689 751 784 82  506.5
    464 2   760 704 763 514 554 520 564 82  486
    628 1   762 789 783 618 610 645 625 82  536')

melt(df, id = 1:2, measure.vars = patterns('^x', '^y', '^z'),
     variable.name = 'repeat', value.name = c('x', 'y', 'z'))
#      ID Group repeat   x   y     z
#  1: 144     1      1 566 563 599.0
#  2: 167     2      1 697 682 718.0
#  3: 247     4      1 643 669 698.0
#  4: 317     4      1 633 520 559.0
#  5: 344     3      1 651 589 667.0
#  6: 352     2      1 578 536 591.0
#  7: 382     1      1 678 555 521.0
#  8: 447     3      1 668 663 784.0
#  9: 464     2      1 760 514 564.0
# 10: 628     1      1 762 618 625.0
# 11: 144     1      2 613 549  82.0
# 12: 167     2      2 638 695  82.0
# ...

【讨论】:

  • 您好!谢谢你的建议。但是我不太了解measure.vars = patterns('^x', '^y', '^z') 部分。我的实际变量名称比 x、y 和 z 稍微复杂一些。它们更像:MedianRTPosTrialsPart1、MedianRTPosTrialsPart2 和 MedianRTPosTrialsPart3。我尝试使用这个:measure.vars = patterns('^MedianRTPosTrialsPart'),但这不起作用:Error: measure variables not found in data: ^MedianRTPostrialsPart。还有更多提示吗?
  • 我不确定您的数据是什么样的。但是您可以查看?data.table::melt 以了解如何使用patterns 或列名列表以及以下示例指定measure.var。如果您仍然有问题,您可以通过编辑原始 POST 向我展示您真实数据的简短示例,让我帮助您,或者您可以提出一个新问题。
【解决方案2】:

我会使用reshape

vars <- names(df)[grepl("(x|y|z)",names(df))]

res <- reshape(df, varying=vars, v.names = c("x","y","z"), direction = "long")

head(res)
#     ID Group time   x   y   z id
#1.1 144     1    1 566 613 597  1
#2.1 167     2    1 697 638 756  2
#3.1 247     4    1 643 698 730  3
#4.1 317     4    1 633 646 641  4
#5.1 344     3    1 651 678 708  5
#6.1 352     2    1 578 702 671  6

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-09
    • 2014-11-14
    • 1970-01-01
    • 2017-06-16
    相关资源
    最近更新 更多