Given
x<-data.frame(age=sample(1:10), gender=c("M","F"))
How do I select the rows with gender 'M', and order by age ?
I know I can order the dataframe by age with : x[order(x$age),]
And I can select the rows with gender 'M' with: x[x$gender=='M',]
And finally, I can do both with: y<-x[x$gender=='M',] y<-y[order(y$age),]
Is there a way to more concisely do this ?
I've tried
x[x$gender=='M' & order(x$age),]
and
x[x$gender=='M' & order(x$age),]
which filters but does not order.
> x[x$gender=='M' &order(x$age),]
age gender
1 4 M
3 6 M
5 3 M
7 1 M
9 9 M
I've also tried
x[x$gender=='M' &&order(x$age),]
Which doesn't seem to filter or order (I confess I don't understand the difference between & and &&).
age gender
1 4 M
2 5 F
3 6 M
4 2 F
5 3 M
6 10 F
7 1 M
8 8 F
9 9 M
10 7 F
What am I doing wrong ?
Note: My question is slightly different from the one asked here; I'm specifically trying to understand why my approach(es) don't work. That question asks for an answer but neither it nor its answers help to explain what was wrong with the specific approaches tried here.