alpac
alpac

Reputation: 1

Ordering a data frame by one column's values in R

this may be a little simple but i cannot manage to do it !

I've got a dataframe that looks like this:

        Fruits    gr
Apples   Oranges   4   
Oranges  Lemons    5
Lemons   Apples    2 

And I want it to look like this:

        Fruits    gr
Apples   Apples     2
Oranges  Oranges    4
Lemons   Lemons     5 

So to reorder the twos columns according to the rows. Also Knowing that i have a lot of rows so i cannot move it "manually".

structure(list(Fruits = structure(1:3, .Label = c("apple", "lemons", 
"oranges"), class = "factor"), gr = c(4, 5, 2)), .Names = c("Fruits", 
"gr"), row.names = c("oranges", "apple", "lemons"), class = "data.frame")

Upvotes: 0

Views: 77

Answers (1)

Andre Elrico
Andre Elrico

Reputation: 11480

You can solve this issue like this:

df being your real data.frame.

df[]<-df[match(rownames(df),df$Fruits),]

Upvotes: 2

Related Questions