banner



How To Change The Order Of Columns In R

How to change the club of columns in an R data frame?


Ordering columns might exist required when nosotros want to manipulate the information. Manipulation can have several reasons such as cross verification, visualisation, etc. We should also exist careful when we change anything in the original information because that might impact our processing. To change the order of columns we can utilize the single square brackets.

Example

Consider the below data frame −

> gear up.seed(1) > Class<-letters[1:twenty] > Grade<-rep(c("A","B","C","D"),times=5) > Score<-sample(1:100,xx,replace=Truthful) > df<-data.frame(Class,Grade,Score) > df   Class Grade Score 1   a     A     68 ii   b     B     39 3   c     C      1 4   d     D     34 5   due east     A     87 six   f     B     43 7   g     C     14 eight   h     D     82 9   i     A     59 10  j     B     51 11  m     C     97 12  50     D     85 13  m     A     21 xiv  n     B     54 15  o     C     74 16  p     D      7 17  q     A     73 18  r     B     79 19  south     C     85 xx  t     D     37 > df<-df[, c("Grade","Class","Score")] > df   Form Class Score 1   A     a    68 two   B     b    39 3   C     c     1 4   D     d    34 5   A     e    87 six   B     f    43 7   C     1000    14 8   D     h    82 9   A     i    59 10  B     j    51 eleven  C     yard    97 12  D     l    85 thirteen  A     thousand    21 14  B     n    54 15  C     o    74 16  D     p     7 17  A     q    73 xviii  B     r    79 19  C     due south    85 xx  D     t    37

If the cavalcade names are in an gild as shown below −

> x1<-letters[1:xx] > x2<-rep(c("A","B","C","D"),times=five) > x3<-sample(one:100,20,supercede=Truthful) > df<-data.frame(x3,x2,x1) > df    x3 x2 x1 1  44 A  a two  25 B  b three  70 C  c  4  39 D  d five  51 A  e 6  42 B  f seven   6 C  g viii  24 D  h 9  32 A  i 10 14 B  j xi  2 C  m 12 45 D  50 13 xviii A  yard 14 22 B  n 15 78 C  o 16 65 D  p 17 70 A  q 18 87 B  r 19 70 C  s 20 75 D  t

At present we tin use sort function to change the order as shown beneath −

> df[,sort(names(df))]    x1 x2 x3 1   a  A 44 2   b  B 25 iii   c  C 70 4   d  D 39 v   e  A 51 half dozen   f  B 42 7   g  C 6 viii   h  D 24 9   i  A 32 x  j  B 14 xi  thousand  C  two 12  fifty  D 45 13  thou  A eighteen fourteen  n  B 22 15  o  C 78 xvi  p  D 65 17  q  A 70 18  r  B 87 19  s  C 70 xx  t  D 75

raja

Published on 10-Aug-2020 12:23:18

  • Related Questions & Answers
  • How to change the row club in an R information frame?
  • How to reorder the columns in an R data frame?
  • How to notice the class of columns of an R information frame?
  • How to standardize columns in an R data frame?
  • How to change the order of one cavalcade information frame and get the output in information frame format in R?
  • How to find the groupwise gild of values in an R data frame?
  • How to find the median of all columns in an R data frame?
  • How to find the number of numerical columns in an R information frame?
  • How to catechumen columns of an R data frame into rows?
  • How to subset gene columns in an R information frame?
  • How to concatenate numerical columns in an R data frame?
  • How to create histogram of all columns in an R data frame?
  • How to find the absolute altitude betwixt columns of an R data frame?
  • How to add a prefix to columns of an R data frame?
  • How to change the proper noun of a data frame in R?

Source: https://www.tutorialspoint.com/how-to-change-the-order-of-columns-in-an-r-data-frame

Posted by: carleparld1998.blogspot.com

0 Response to "How To Change The Order Of Columns In R"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel