Adding a column with consecutive numbers in R

后端 未结 3 1526
不思量自难忘°
不思量自难忘° 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:56

    For a dataframe (df) you could use

    df$observation <- 1:nrow(df) 
    

    but if you have a matrix you would rather want to use

    ma <- cbind(ma, "observation"=1:nrow(ma)) 
    

    as using the first option will transform your data into a list.

    Source: http://r.789695.n4.nabble.com/adding-column-of-ordered-numbers-to-matrix-td2250454.html

提交回复
热议问题