Reputation: 3039
I can easily order a dataframe like this:
df<-data.frame(a=c(1,2,5,7,2),b=(1:5))
# indexing by column name, works
df2<-df[with(df,order(a)),]
But how can I achive the same by indexing with the column number?
# indexing with column number, attemps failed
df2<-df[with(df,order(colnames(df[1]))),]
df2<-df[with(df,order(deparse(colnames(df[1])))),]
df2<-df[with(df,order(paste(colnames(df[1])))),]
df2<-df[with(df,order(paste(deparse(colnames(df[1]))))),]
Upvotes: 1
Views: 104
Reputation: 49033
Like this ?
df2 <- df[order(df[,1]),]
If you use with
you won't be able to use column numbers.
Upvotes: 1