-4

I am desperately trying to sort a dataframe ("transp_correlations) by one column "corr" (ideally in descending order). Waht I need to be returned is the complete dataframe (incl. its rownames).

Unfortunately I face a lot of issues - what I tried is

  • transp <- transp_correlations[order(corr), ] --> which returns a scalar (the first cell of the dataframe)
    • transp <- transp_correlations[order(transp_correlations$corr), ] --> which returns a sorted vector but deletes the rownames
  • transp <- transp_correlations[order("corr") ] --> which returns a non-sorted data frame

According to my knowledge the first option is the correct syntax and I identified the other two only by chance. Any guidance how to write a correct syntax would be highly appreciated.

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")

Mr Bueff
  • 3
  • 2
  • Welcome to Stack Overflow. It will be impossible for us to assist with this question without a sample of data that reproduces your error. Please read http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example for hints on providing reproducible examples. I'm unable to replicate the behavior you describe with the `mtcars` data set. Would you be able to share a snippet of your data using `dput(head(transp_correlations, 20))`? – Benjamin Nov 15 '16 at 17:36
  • Thanks ... and sure, please find the snippet below. And as I said I am desperate about this challenge but wouldn't the most logic explanation somehow related to some different behaviours in the R versions? I am using R version 3.2.3 – Mr Bueff Nov 28 '16 at 14:08

1 Answers1

1

Using your data, I'll try to explain what seems to have occurred with your attempts.

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")

The following line fails for me because there is no object named corr in my workspace. This line of code has not directed R to use the corr column of your data frame, so if it isn't failing for you, then you must have defined an object named corr in your workspace. R is attempting to order transp_correlations based on that external corr, which is almost certainly not what you want.

transp_correlations[order(corr), ] 

This next line gives me the result I believe you want, except that it is ordered the wrong way.

transp_correlations[order(transp_correlations$corr), ]

This third line gives me the corr column of your data frame. You've ordered the character vector c("corr"), which returns itself. So you've only extracted one column.

transp_correlations[order("corr") ] 

To get the result you desire, try

transp_correlations[order(transp_correlations$corr, decreasing = FALSE), ]
Benjamin
  • 16,897
  • 6
  • 45
  • 65