订购数据框时遇到的挑战

时间:2016-11-15 16:08:45

标签: r

我拼命想要将数据帧(" transp_correlations)排序为一列" corr" (理想情况下按降序排列)。我需要返回的是完整的数据框(包括它的rownames)。

不幸的是我遇到了很多问题 - 我尝试的是

  • transp< - transp_correlations [order(corr),] - >返回一个标量(数据帧的第一个单元格)
    • transp< - transp_correlations [order(transp_correlations $ corr),] - >它返回一个已排序的向量,但删除了rownames
  • transp< - transp_correlations [order(" corr")] - >返回一个未排序的数据框

根据我的知识,第一个选项是正确的语法,我只是偶然发现了另外两个。任何有关如何编写正确语法的指导都将受到高度赞赏。

结构(列表(corr = c(-0.117661691889059,-0.244374051965591, -0.219277156640164,-0.162119035544463,-0.153750058161601,0.238059055122716, -0.112988647529562,-0.22890950435724,-0.212213257871138,-0.164868801623183, -0.157327010028056,0.276036409162576,-0.129156901338342,-0.247314062599315, -0.234716884100804,-0.177925630373387,-0.164633635391903,0.227786973830269, -0.10921452701353,-0.0385193717439635),abscorr = c(0.117661691889059, 0.244374051965591,0.219277156640164,0.162119035544463,0.153750058161601, 0.238059055122716,0.121288647529562,0.22890950435724,0.212213257871138, 0.164868801623183,0.157327010028056,0.276036409162576,0.129156901338342, 0.247314062599315,0.234716884100804,0.177925630373387,0.164633635391903, 0.227786973830269,0.10921452701353,0.0385193717439635),rang = c(62L, 108L,96L,85L,80L,105L,61L,100L,94L,89L,82L,115L,70L, 110L,103L,91L,88L,98L,59L,36L)),. Name = c(" corr"," abscorr", " rang"),row.names = c(" V1"," V2"," V3"," V4&#34 ;," V5"," V6"," V7"," V8", " V9"," V10"," V11"," V12"," V13"," V14& #34;," V15"," V16"," V17"," V18"," V19",& #34; V20" ),class =" data.frame")

1 个答案:

答案 0 :(得分:1)

使用您的数据,我会尝试解释您的尝试似乎发生了什么。

transp_correlations <- structure(list(corr = c(-0.117661691889059, -0.244374051965591, -0.219277156640164, -0.162119035544463, -0.153750058161601, 0.238059055122716, -0.112988647529562, -0.22890950435724, -0.212213257871138, -0.164868801623183, -0.157327010028056, 0.276036409162576, -0.129156901338342, -0.247314062599315, -0.234716884100804, -0.177925630373387, -0.164633635391903, 0.227786973830269, -0.10921452701353, -0.0385193717439635), abscorr = c(0.117661691889059, 0.244374051965591, 0.219277156640164, 0.162119035544463, 0.153750058161601, 0.238059055122716, 0.112988647529562, 0.22890950435724, 0.212213257871138, 0.164868801623183, 0.157327010028056, 0.276036409162576, 0.129156901338342, 0.247314062599315, 0.234716884100804, 0.177925630373387, 0.164633635391903, 0.227786973830269, 0.10921452701353, 0.0385193717439635), rang = c(62L, 108L, 96L, 85L, 80L, 105L, 61L, 100L, 94L, 89L, 82L, 115L, 70L, 110L, 103L, 91L, 88L, 98L, 59L, 36L)), .Names = c("corr", "abscorr", "rang"), row.names = c("V1", "V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10", "V11", "V12", "V13", "V14", "V15", "V16", "V17", "V18", "V19", "V20" ), class = "data.frame")

以下行对我失败,因为我的工作区中没有名为corr的对象。这行代码没有指示R使用数据框的corr列,因此如果它没有失败,那么您必须在工作区中定义一个名为corr的对象。 R正在尝试根据外部transp_correlations订购corr,这几乎肯定不是您想要的。

transp_correlations[order(corr), ] 

下一行给出了我认为你想要的结果,除了它以错误的方式排序。

transp_correlations[order(transp_correlations$corr), ]

第三行为我提供了数据框的corr列。您已经订购了返回自身的字符向量c("corr")。所以你只提取了一列。

transp_correlations[order("corr") ] 

要获得您想要的结果,请尝试

transp_correlations[order(transp_correlations$corr, decreasing = FALSE), ]