Referencing Row Number in R

前端 未结 4 1466
温柔的废话
温柔的废话 2021-02-04 01:53

How do I reference the row number of an observation? For example, if you have a data.frame called \"data\" and want to create a variable data$rownumber

4条回答
  •  南旧
    南旧 (楼主)
    2021-02-04 02:31

    This is probably the simplest way:

    data$rownumber = 1:dim(data)[1]
    

    It's probably worth noting that if you want to select a row by its row index, you can do this with simple bracket notation

    data[3,]
    
    vs.
    
    data[data$rownumber==3,]
    

    So I'm not really sure what this new column accomplishes.

提交回复
热议问题