0

I currently have the following dataframes:

> corr
          i4        i3        i2
i1 0.7965509 0.8198761 0.7941727
> newcor
   i4 i3 i2
i1  0  0  0

I would like to combine these dataframes into one dataframe that sorts the corr values from greatest to least from left to right and puts the corresponding newcor values in the following row so that the final result is:

> newcor
        i3        i4        i2
i1 0.8198761 0.7965509 0.7941727
i1 0.0000000 0.0000000 0.0000000

It is just a coincidence that all the values in the 2nd row are 0s, and they must lineup according to initial values in newcor. For example in this case the first newcor would be:

> newcor
   i3 i4 i2
i1  0  0  0

1 Answer 1

2

Try

 corr1 <- corr[,order(-corr)]
 rbind(corr1, newcor[names(corr1)])

Or can be combined to a single line as @Frank suggested if the initial data columns are in the same order

 rbind(corr,newcor)[,order(-corr)]
Sign up to request clarification or add additional context in comments.

5 Comments

Or rbind(corr,newcor)[,order(-corr)]
@Frank I guess that needs the assumption that the column names are the in the order in the initial dataset
@akrun that worked; however, the second row becomes named i11 as such: > rbind(corr1, newcor[names(corr1)]) i3 i4 i2 i1 0.8198761 0.7965509 0.7941727 i11 0.0000000 0.0000000 0.0000000 How can I make it so both of these rows have the same name always? Thanks!
@costebk08 Row names in a data.frame must be distinct. If you were using a matrix, I guess that wouldn't be a problem.
@costebk08 as Frank mentioned, it is a feature in data.frame.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.