Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a column with consecutive numbers in R

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 observations, for example, starting with 1 for the first observation, and ending with 200 on the last one).

How can I do this?

like image 857
Emily Avatar asked Apr 23 '15 15:04

Emily


1 Answers

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

like image 173
Alex Avatar answered Sep 18 '22 13:09

Alex