-1

I am trying to order a data frame/matrix by the variable VIRUS, by descending or ascending it does not matter.

enter image description here

Anncol<-data.frame(Metadata$VIRUS) ##From left to case, add more if neeedd
sortedAnncol<-Anncol[order(Anncol$VIRUS),]
sAnncol<-as.matrix(sortedAnncol)

enter image description here

This is what I have tried so far, but I lose the first column of data, the corresponding data points in the data frame. How can order the 'Anncol' data frame by the variable 'VIRUS' while simultaneously ordering the rest of the data frame.

Any help would be greatly appreciated! Thank you in advance

Phil
  • 7,287
  • 3
  • 36
  • 66
  • Why are you transfoming in a matrix? – Vinícius Félix Dec 04 '22 at 23:40
  • Because 'sortedAnncol' saves under Values which does not allow me to access it as a table. This may be a mistake, but transforming it into a matrix allows me to view the ordered VIRUS values at least, even if the first column of data is missing. Maybe I am doing it all wrong, please give me any suggestions. Thank you! – Angles Agar Dec 04 '22 at 23:44

1 Answers1

0

Here a solution using dplyr

library(dplyr)
Metadata <- Metadata %>% arrange(VIRUS)
Vinícius Félix
  • 8,448
  • 6
  • 16
  • 32
  • Hello and thank you. However this just prints out the arranged list in the console but does not update the actual order of the list. How can I fix this to make it actually update? – Angles Agar Dec 06 '22 at 16:24
  • You need to overwrite the data.frame @AnglesAgar, I updated my code. – Vinícius Félix Dec 06 '22 at 17:29