Subsetting and Ordering in R I have a random created dataframe named a:

  group marks  upd
1     a     2   up
2     b     3   up
3     a     4 down
4     b     5   up
5     a     6 down
6     c     7   up
7     a     8 down


I wanted to subset it with only group='a' and marks in decreasing order of a. I used this query but the marks are not arranged in decreasing order. What is the error in my code?

Here is my code and output:

&gt; a[a$group=="a" &amp; a$marks[order(a$marks,decreasing=T)],]
  group marks  upd
1     a     2   up
3     a     4 down
5     a     6 down
7     a     8 down

Warning message:
In a$group == "a" &amp; order(a$marks, decreasing = TRUE)[1:3] :
  longer object length is not a multiple of shorter object length
a$group=="a" gives 

# [1]  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE


and a$marks[order(a$marks,decreasing=T)] gives 

# [1] 8 7 6 5 4 3 2


Combining those to using a logical and (&amp;) does not make much sense here. TRUE &amp; 8 is TRUE - the subsetting may accidentally work. But you want order to look at your subset only, not at all rows in a.  

Try for instance

newa &lt;- a[a$group=="a", ] 
newa[order(newa$marks, decreasing = TRUE), ]
#   group marks  upd
# 7     a     8 down
# 5     a     6 down
# 3     a     4 down
# 1     a     2   up


or, more convenient and readable:

library(dplyr)
a %&gt;% filter(group=="a") %&gt;% arrange(desc(marks))