I have a df :
Years Company ID Name Return
2003 23 622009 a 2323123
2008 14 622008 v 2312343
2009 43 622007 c 35345345
2006 32 622006 d 45656554
2005 67 622005 f 756756766
1999 60 622004 y 4564533
2001 63 622003 i 6756766
2002 66 622002 m 32423412
2008 68 622001 o 934833833
2004 48 622000 e 34132222
I am trying to rearrange the ordering of the Years column in ascending order (so the most recent year (2009) is on the top). In example :
Years Company ID Name Return
2009 62 622007 c 35345345
2008 62 622008 v 2312343
2008 62 622001 o 934833833
2006 62 622006 d 45656554
2005 62 622005 f 756756766
and so on…
I tried to do it with a for loop but it gives an incorrect answer. I get only the last row :
2005 62 622005 f 756756766
for (i in 1:length(unique(df$Year))){
mm<-df[df$Year==unique(df$Year)[i],]
}
Could someone please guide me what I am doing wrong?
Use
orderfunction for that: