user2603386
user2603386

Reputation:

Reorder data frame values

I have a data frame (titled EMG), which has values (which, for example, go from a to u), which are organized like this:

a d g j m p s
b e h k n q t
c f i l o r u

Instead, I want it to be organized like this

a b c d e f g
h i j k l m n
o p q r s t u

I can't use transpose, because that would transpose the whole data frame. Is there any way to reorder the values horizontally instead of vertically?

Thanks

Upvotes: 1

Views: 123

Answers (2)

Nishanth
Nishanth

Reputation: 7130

Assuming your data frame is df, try:

t(matrix(unlist(df), nrow=ncol(df))) 

Upvotes: 1

mnel
mnel

Reputation: 115465

If all your columns are of the same type, then you can unlist the data frame, create a matrix that is filled by row (not the default by column) and then re-coerce to data.frame.

eg

as.data.frame(matrix(unlist(EMG),nrow=3,byrow=TRUE))
##   V1 V2 V3 V4 V5 V6 V7
## 1  a  b  c  d  e  f  g
## 2  h  i  j  k  l  m  n
## 3  o  p  q  r  s  t  u

Upvotes: 4

Related Questions