this question has answer here:
- how sort dataframe column(s)? 15 answers
i trying order rows variable. have created sample data frame below , tried order rows ordering not appear work.
# create vectors data frame score <- rep(seq(1:3), 2) id <- rep(c(2014, 2015), each = 3) var_if_1 <- rep(c(0.1, 0.8), each = 3) var_if_2 <- rep(c(0.9, 0.7), each = 3) var_if_3 <- rep(c(0.6, 0.2), each = 3) # generate , print data frame of raw data foo <- data.frame(score, id, var_if_1, var_if_2, var_if_3) foo # impose arbitrary ordering bar <- foo[sample(1:nrow(foo)), ] bar # order rows increasing on 'score' bar[order(score), ]
what doing wrong doesn't oder rows on score?
you should use
bar[order(bar$score), ]
otherwise, you're ordering on base of variable "score" instead of column.
Comments
Post a Comment