[R] Sorting a data frame

BORGULYA Gábor borgulya at gyer2.sote.hu
Thu Jul 31 12:42:15 CEST 2003


Hi!

I think this is an important example! Is there a way to make it included 
in the help of order?

Maybe a shortened version:

# sorting a data frame
df <- data.frame(V1 = c("W","A", "A", "B", ""), V2 = c("E", "M", "B", 
"O", "Q"))
sorted <- df[order(df$V1, df$V2), ]


And a question: what to do to have the sorted data frame with the row 
labels 1:n?

Gábor

----------------------------------------------------------------
Marc Schwartz írta:
> On Wed, 2003-07-16 at 08:42, Wayne Jones wrote:
>>Does anyone know if it is possible to sort a dataframe?

> Example:

# Create two column df
df <- data.frame(V1 = c("W","A", "A", "B", ""), V2 = c("E", "M", "B", 
"O", "Q"))
# show df unsorted
df

   V1 V2
1  W  E
2  A  M
3  A  B
4  B  O
5     Q

# now sort df, by V1, then V2
df[order(df$V1, df$V2), ]

   V1 V2
5     Q
3  A  B
2  A  M
4  B  O
1  W  E




More information about the R-help mailing list