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
.
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 is10
, then:df$numeracao <- 1:10
– neves
thanks for the reply
– Izak Mandrak