Auto increment in R

Asked

Viewed 110 times

2

How do I generate an auto incremental ID field (1,2,3...) in a data.frame in R that in a new column?

meuDataFrase<- as_tibble(unique(tabelImportada$ColunaX))

I need to create a new column on that date.frame that has the line numbers because I will record this information in a dimensional table in postgres.

2 answers

4


One way to do this is to use the function rownames_to_column() package .

The example below includes ids for table rows iris. In this case it creates a column called "rowname".

library(tidyverse)
# só para imprimir melhor
tbl_iris <- as_tibble(iris)

tbl_iris %>% 
     rownames_to_column()
# A tibble: 150 x 6
   rowname Sepal.Length Sepal.Width Petal.Length Petal.Width Species
   <chr>          <dbl>       <dbl>        <dbl>       <dbl> <fct>  
 1 1                5.1         3.5          1.4         0.2 setosa 
 2 2                4.9         3            1.4         0.2 setosa 
 3 3                4.7         3.2          1.3         0.2 setosa 
 4 4                4.6         3.1          1.5         0.2 setosa 
 5 5                5           3.6          1.4         0.2 setosa 
 6 6                5.4         3.9          1.7         0.4 setosa 
 7 7                4.6         3.4          1.4         0.3 setosa 
 8 8                5           3.4          1.5         0.2 setosa 
 9 9                4.4         2.9          1.4         0.2 setosa 
10 10               4.9         3.1          1.5         0.1 setosa 
# … with 140 more rows

Another alterative is to create a column with the name you want using mutate() of combined with seq_len().

tbl_iris %>% 
  mutate(id = seq_len(nrow(.)))
# A tibble: 150 x 6
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species    id
          <dbl>       <dbl>        <dbl>       <dbl> <fct>   <int>
 1          5.1         3.5          1.4         0.2 setosa      1
 2          4.9         3            1.4         0.2 setosa      2
 3          4.7         3.2          1.3         0.2 setosa      3
 4          4.6         3.1          1.5         0.2 setosa      4
 5          5           3.6          1.4         0.2 setosa      5
 6          5.4         3.9          1.7         0.4 setosa      6
 7          4.6         3.4          1.4         0.3 setosa      7
 8          5           3.4          1.5         0.2 setosa      8
 9          4.4         2.9          1.4         0.2 setosa      9
10          4.9         3.1          1.5         0.1 setosa     10
# … with 140 more rows
  • You’d better use the row_number function()

2

Always remember to bring a reproducible example. I created a DF simple as an example, but the concept is the same for others DF's.

Code:

x <- data.frame(z = c(51:101), y = c(50:100))

for(i in 1:nrow(x)) {
    
    x[i,3] <- i
    
}

Your case rownames are not numbers, use the following command before:

rownames(x) <- NULL

Note: I placed the column 3 in the for(), but you must put the number of the column you want the number of the row to appear.

Browser other questions tagged

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