-6

For example, now I have:

A 1 1 1 1 3 4
B 1 8 9 7 9 6
A 2 3 4 5 6 6
C 1 1 1 1 1 1.

I want to get the rows with same letter A together, like this:

A 1 1 1 1 3 4
A 2 3 4 5 6 6

What should I do? It's in R programming.

the letters in front of every row is not the name of row, just the first element in each row

  • Please post a reproducible example of your data (for instance, are A, B, A, C a column or row names?). If you're trying to sort your data by the letter, you should check out the `order` function. – josliber Sep 11 '14 at 21:53
  • `df[df[, 1] == "A", ]`, if `df` is your data set – David Arenburg Sep 11 '14 at 22:02
  • @josilber, I doubt they are row names, as duplicate row names are not allowed. – David Arenburg Sep 11 '14 at 22:05
  • 1
    @DavidArenburg `mat <- matrix(1:4, nrow=2) ; rownames(mat) <- c("A", "A")` does not generate an error, so it is possible. Either way, best if the OP posts a reproducible example. – josliber Sep 11 '14 at 22:07
  • @josilber, this is interesting, as it will generate an error if `mat` is a `data.frame` – David Arenburg Sep 11 '14 at 22:13
  • sorry for the confusion. the letters in front of every row is not the name of row, just the first element in each row – Star Young Sep 14 '14 at 20:14

1 Answers1

0

You can use the order() function to do that:

# reading the data
df <- read.table(header=FALSE, text="A 1 1 1 1 3 4
B 1 8 9 7 9 6
A 2 3 4 5 6 6
C 1 1 1 1 1 1")

# ordering the data according to the values in the first colum
df[order(df[,1]),]
Jaap
  • 81,064
  • 34
  • 182
  • 193