R: Reorder strings

Assume a data frame constructed as

df = data.frame(replicate(2,sample(0:10,2,rep=TRUE)))
df = t(df)
df

which returns something like

   [,1] [,2]
X1    9    8
X2   10    4

Here's the question: how to change the order of the lines so that:

   [,1] [,2]
X2   10    4
X1    9    8

? Thanks!

+4
source share
4 answers

One way to do this is to index the string directly:

df[c("X2","X1"),]

Or you can use row indices:

df[c(2,1),]  # or just df[2:1,]
+4
source

You can also try

df[nrow(df):1,]

This arranges the rows in reverse order for any data frame df.

+1
source

heyy :

(nameofthedataframe, Select = c ( NAme))

0

@nimrodm, , "8" "9". "n" .

mydata [c (1: 7,9,8,10: n),]

-1

Source: https://habr.com/ru/post/1607532/


All Articles