i have dataframe 5 columns , large dataset. want sort column 3. how sort after first row. (when calling function want end nrows)
example output:
original:
4 7 9 6 8 new:
4 9 8 7 6 thanks!
if i'm correctly understanding want do, approach should work:
z <- data.frame(x1 = seq(10), x2 = rep(c(2,3), 5), x3 = seq(14, 23)) zsub <- z[2:nrow(z),] zsub <- zsub[order(-zsub[,3]),] znew <- rbind(z[1,], zsub) basically, snip off rows want sort, sort them in descending order on column 3, reattach first row.
and here's piped version using dplyr, don't clutter workspace objects:
library(dplyr) z <- z %>% slice(2:nrow(z)) %>% arrange(-x3) %>% rbind(slice(z, 1), .)
Comments
Post a Comment