26103

Sort each row in a dataframe by value of column

Say I have a dataframe like this:

Date A B C D E H 1/28/2013 56 51 35 44 08 18 1/25/2013 38 56 28 39 23 32 1/21/2013 36 51 45 25 40 08

what I want to do is sort each row's ABCDE column by their values. So I can get is:

Date A B C D E H 1/28/2013 08 35 44 51 56 18 1/25/2013 23 28 38 39 56 32 1/21/2013 25 36 40 45 51 08

Answer1:

You can use apply:

dat[c("A", "B", "C", "D", "E")] <- t(apply(dat[c("A", "B", "C", "D", "E")], 1, sort)) # Date A B C D E H # 1 1/28/2013 8 35 44 51 56 18 # 2 1/25/2013 23 28 38 39 56 32 # 3 1/21/2013 25 36 40 45 51 8

where dat is the name of your data frame.

Recommend

  • array of string pointers in C
  • Selection in ListBox on an Excel worksheet goes blank everytime I do something, why?
  • EF 4.1 Code First bug in Find/Update. Any workaround? Should it be reported?
  • Transpose table then set and rename index
  • jinja2 template not found and internal server error
  • remove unicode characters but keep all special and English characters with preg_replace
  • For loop with if condition on multiple R functions
  • Can't remove headers after they are sent
  • Floated image with variable width and heading with background image
  • R convert summary result (statistics with all dataframe columns) into dataframe
  • as3-flash: any way to access all the instances placed in different frames from document class?
  • Make new pandas columns based on pipe-delimited column with possible repeats
  • wxPython: displaying multiple widgets in same frame
  • xtable package: Skipping some rows in the output
  • Groovy: Unexpected token “:”
  • How can I sort a a table with VBA with given text condition?
  • Replace value with Factor in r data.table
  • Row Count Is Returning the incorrect number using RaptureXML
  • Is it possible to access block's scope in method?
  • Jetty Server not starting: Unable to establish loopback connection
  • How to access EntityManager inside Entity class in EJB3
  • R - Combining Columns to String Based on Logical Match
  • Repeat a vertical line on every page in Report Builder / SSRS
  • Illegal mix of collations for operation for date/time comparison
  • Change an a tag attribute in JavaScript based on screen width
  • Release, debug version and Authorization Google?
  • Unanticipated behavior
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • trying to dynamically update Highchart column chart but series undefined
  • Hits per day in Google Big Query
  • embed rChart in Markdown
  • Can't mass-assign protected attributes when import data from csv file
  • Does armcc optimizes non-volatile variables with -O0?
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • java string with new operator and a literal