HonestRlover.not.
HonestRlover.not.

Reputation: 67

order multiple columns with same entries alphabetically

I found some questions about ordering multiple columns in R. However, these columns have at least one double entry, for example two times the same name or something like this. My dataframe looks like this

VP Selbst ES        eigForm eigES     andForm andES     MM MMRT MMES
5  3      laut      7       modern    5       sensibel  f  1082 neugierig
5  3      modern    7       neugierig 1       laut      j  801  sensibel
5  4      sensibel  2       laut      3       neugierig j  734  modern
5  2      neugierig 1       sensibel  2       modern    j  496  laut

I want to compare the ratings for the different traits (ES, eigES, andES, MMES) and create a new variable which tells if the ratings are the same for this trait or not. But as you can see they are in a different order every time (the order how they appared in the experiment). So I want to order the entries in these 3 columns alphabetically. How can I do this?

I tried datn[order(datn[,3], datn[,5], datn[,7], datn[,10]),] to get something like this

VP Selbst ES        eigForm eigES     andForm andES     MM MMRT MMES
5  3      laut      2       laut      1       laut      j  496  laut
5  3      modern    7       modern    2       modern    j  734  modern
5  2      neugierig 7       neugierig 3       neugierig f  1082 neugierig
5  4      sensibel  1       sensibel  5       sensibel  j  801  sensibel

but it didn't work

Upvotes: 0

Views: 38

Answers (1)

maydin
maydin

Reputation: 3755

You should separate your dataframe like df[,1:4] , df[,5:7] etc at the beginning. Then you can use join(), merge() etc like merge(df[,1:4] ,df[,5:7], by=...)

Alternatively, you can use order() function for each separated dataframes, and after ordering each one by using cbind() you can bind them as well.

Upvotes: 1

Related Questions