I have a simple dataframe as shown below:
structure(list(DAILY_INJ_DATE = c("2018-01", "2018-02", "2018-03",
"2018-04", "2018-05", "2018-06", "2018-07", "2018-08", "2018-09",
"2018-10", "2018-11", "2018-12", "2019-01", "2019-02", "2019-03",
"2019-04", "2019-05", "2019-06", "2019-07", "2019-08", "2019-09",
"2019-10", "2019-11", "2019-12", "2020-01", "2020-02", "2020-03",
"2020-04", "2020-05", "2020-06", "2020-07", "2020-08", "2020-09",
"2020-10", "2020-11", "2020-12", "2018-01", "2018-02", "2018-03",
"2018-04", "2018-05", "2018-06", "2018-07", "2018-08", "2018-09",
"2018-10", "2018-11", "2018-12", "2019-01", "2019-02", "2019-06",
"2019-07", "2019-08", "2019-09", "2019-10", "2019-11", "2019-12",
"2020-01", "2020-02", "2020-03", "2020-04", "2020-05", "2020-06",
"2020-07", "2020-08", "2020-09", "2020-10", "2020-11", "2020-12"
), PID = c("42135311180000", "42135311180000", "42135311180000",
"42135311180000", "42135311180000", "42135311180000", "42135311180000",
"42135311180000", "42135311180000", "42135311180000", "42135311180000",
"42135311180000", "42135311180000", "42135311180000", "42135311180000",
"42135311180000", "42135311180000", "42135311180000", "42135311180000",
"42135311180000", "42135311180000", "42135311180000", "42135311180000",
"42135311180000", "42135311180000", "42135311180000", "42135311180000",
"42135311180000", "42135311180000", "42135311180000", "42135311180000",
"42135311180000", "42135311180000", "42135311180000", "42135311180000",
"42135311180000", "42135335900000", "42135335900000", "42135335900000",
"42135335900000", "42135335900000", "42135335900000", "42135335900000",
"42135335900000", "42135335900000", "42135335900000", "42135335900000",
"42135335900000", "42135335900000", "42135335900000", "42135335900000",
"42135335900000", "42135335900000", "42135335900000", "42135335900000",
"42135335900000", "42135335900000", "42135335900000", "42135335900000",
"42135335900000", "42135335900000", "42135335900000", "42135335900000",
"42135335900000", "42135335900000", "42135335900000", "42135335900000",
"42135335900000", "42135335900000"), InjIndex = c(3.1488349310755e-05,
7.16470821042452e-05, 3.08198068625437e-05, 0.00365977544989287,
0.000102146739534363, 6.97288098968181e-05, 6.67030385322113e-05,
0.000101198808641258, 6.96471158898905e-05, 0.000100457907956119,
0.002770103468248, 0.000141272149337637, 3.71747211895911e-05,
0, 0, 0, NA, NA, 0.00261196063305948, 0.0020329847793613, 0.0268256888287629,
0.0190615086256689, 0.00165037617202441, 0.00823890291192408,
0.0149562009694358, 3.82198063529811e-05, 0.00703837718531629,
0.0460765131610604, 0.0571638755572333, 0.0600559821857274, 0.0636357826177028,
0.0643659884529977, 0.0577969845601966, 0.0588167585535698, 0.0593479205060031,
0.0478238114640216, 0.0579565073781893, 0.0439869629670818, 0.056714771440236,
0.122274207049878, 0.136105301010138, 0.133225772135695, 0.126920643583703,
0.128496063591315, 0.14043302451169, 0.113191351198699, 0.125443452699286,
0.146339474772728, 0.0191599802822513, NA, 0.133221262910392,
0.216814720357711, 0.606926958546271, NA, NA, 0.131402308568841,
NA, 0.355567523506574, NA, 0.0234750006884004, 0.0416741137140514,
NA, NA, 0.0585083175072382, NA, 0.0852075310970539, 0.0691143041976479,
NA, NA)), row.names = c(NA, 69L), class = "data.frame")
I intend to sort this table based first on increasing 'PID' column and then descending order of 'Daily_Inj_Date' column.
I used the below syntax as:
df1 <- df[order(df$PID, -as.numeric(df$DAILY_INJ_DATE)),]
but the result I get is incorrect. For ex., the last date for the PID ='42135311180000' , is '2020-12' but after performing the ordering,the resultant data frame shows:
