Adding a column with consecutive numbers in R

后端 未结 3 1519
不思量自难忘°
不思量自难忘° 2021-02-13 14:14

I apologize if this question is abhorrently simple, but I\'m looking for a way to just add a column of consecutive integers to a data frame (if my data frame has 200 observation

3条回答
  •  孤城傲影
    2021-02-13 14:47

    Probably, function tibble::rowid_to_column is what you need if you are using tidyverse ecosystem.

    library(tidyverse)
    dat <- tibble(x=c(10, 20, 30), 
                  y=c('alpha', 'beta', 'gamma'))
    dat %>% rowid_to_column(var='observation')
    
    # A tibble: 3 x 3
      observation     x y    
              
    1           1    10 alpha
    2           2    20 beta 
    3           3    30 gamma
    

提交回复
热议问题