How to number lines of a data.frame in R?

Asked

Viewed 1,493 times

1

Assuming I have the following date.:

print(DADOS)

letra N1 N2 N3 N4
A     2  3  4  4
A     1  2  3  4
A     2  2  1  3
B     0  1  2  0 
C     4  4  3  2
C     2  2  2  2
D     4  3  2  1
D     1  0  1  4
E     4  4  4  4

How can I number the lines of my data.frame, leaving it like this:

print(DADOS_numerados)

    letra N1 N2 N3 N4 numeracao
    A     2  3  4  4  1
    A     1  2  3  4  2
    A     2  2  1  3  3
    B     0  1  2  0  4
    C     4  4  3  2  5
    C     2  2  2  2  6
    D     4  3  2  1  7
    D     1  0  1  4  8
    E     4  4  4  4  9

I know it sounds like a simple thing, but I’ve tried countless unsuccessful ways.

How can I list each line of my data.frame in R?

  • 1

    The best solution was given by @Carlos Eduardo in the field of answers. In a very archaic way, you can know the 1) number of lines: nrow(df) and then 2) create an array in your database. Suppose the number of rows is 10, then: df$numeracao <- 1:10

  • thanks for the reply

3 answers

4

DADOS <- read.table(text = 
  'letra N1 N2 N3 N4
   A     2  3  4  4
   A     1  2  3  4
   A     2  2  1  3
   B     0  1  2  0 
   C     4  4  3  2
   C     2  2  2  2
   D     4  3  2  1
   D     1  0  1  4
   E     4  4  4  4',
  header = TRUE)

DADOS$numeracao <- 1:nrow(DADOS)

> head(DADOS)
  letra N1 N2 N3 N4 numeracao
1     A  2  3  4  4         1
2     A  1  2  3  4         2
3     A  2  2  1  3         3
4     B  0  1  2  0         4
5     C  4  4  3  2         5
6     C  2  2  2  2         6

But unless you use this variable for some very specific operation, it’s unnecessary; you can simply use the line numbers.

  • thank you for the answer I will test

  • 2

    Oops, my mistake. I had used everything tiny and forgot to change everything by pasting the answer. I will edit it.

4


There are several ways to perform this action. I leave here a contribution with examples using "base" and packet-aided R commands.

  • With resident commands in the R:

As already shown by @Carloseduardolagosta, it is interesting that a vector is previously created so that the operation is more fluid.

To create the vectors, for this case/question, you have the option of using the nrow or seq.int.

numeracao <- 1:nrow(dados) # de 1 ate numero de linhas do data frame dados 
numeracao <- seq.int(nrow(dados))
# seq.int = sequencia de int de 1 ate numero de linhas do data frame dados 

Once this is done, just paste the vector into the data frame of interest with cbind or using brackets [ ] to create a new column.

dados <- cbind(dados, numeracao)
dados[, "numeracao"] <- numeracao
  • With employment of packages:

This operation is also commonly done with the aid of packages like the dplyr and tibble. It is also possible to previously create the vector/column of interest and then proceed with the desired action, however I believe it is more common to create the vector within the package function itself, like this mutate.

library(dplyr)
dados <- dados %>% 
  dplyr::mutate(numeracao = 1:nrow(dados))

bind_cols has the same function as cbind, but its employment enables the creation of a "large" function structure using the dplyr.

dados <- dados %>% 
  dplyr::bind_cols(numeracao = 1:nrow(dados))

The package tibble has a specific function for the creation of this numeracao/id, which is the function rowid_to_column. The function rownames_to_column can also be easily implemented and adapted, but it is worth mentioning the importance of checking the names of the lines (rownames) are numerical and are in the correct order.

library(tibble)
dados <- tibble::rowid_to_column(dados, var = "numeracao")
dados <- tibble::rownames_to_column(dados, var = "numercao")

And using dplyr and tibble it is also possible to do this easily.

dados <- dados %>% 
  tibble::rownames_to_column(var = "numercao")

*Obs.: The pipe (%>%) is an operator of the package magrittr but is commonly associated with dplyr.

  • thanks for the reply, I will test.

3

With the package dplyr, a simple way is as follows.

dados <- dados %>% mutate(numeracao = row_number())

dados
#  letra N1 N2 N3 N4 numeracao
#1     A  2  3  4  4         1
#2     A  1  2  3  4         2
#3     A  2  2  1  3         3
#4     B  0  1  2  0         4
#5     C  4  4  3  2         5
#6     C  2  2  2  2         6
#7     D  4  3  2  1         7
#8     D  1  0  1  4         8
#9     E  4  4  4  4         9

Note that it can also be

dados <- mutate(dados, numeracao = row_number())
  • thanks for the reply, I will test.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.