How to sort specific column in data frame in ascending order with R

1 Answer

0 votes
df = data.frame(a = c(4, 7, 1, 3),
                b = c(98, 12, 87, 20),
                c = c(2, 4, 5, 9),
                d = c(13, 77, 50, 20),
                row.names = c('o', 'p', 's', 't'))
  
print(df)
  
df <- df[with(df, order(a)), ]

print(df)
  
  
  
        
        
# run:
#
#   a  b c  d
# o 4 98 2 13
# p 7 12 4 77
# s 1 87 5 50
# t 3 20 9 20
#   a  b c  d
# s 1 87 5 50
# t 3 20 9 20
# o 4 98 2 13
# p 7 12 4 77
#

 



answered Jul 10, 2021 by avibootz

Related questions

1 answer 158 views
1 answer 234 views
234 views asked Jul 10, 2021 by avibootz
1 answer 269 views
1 answer 199 views
199 views asked Jul 9, 2021 by avibootz
1 answer 222 views
1 answer 201 views
1 answer 196 views
...