在R中添加连续数字的列 [英] Adding a column with consecutive numbers in R

查看:455
本文介绍了在R中添加连续数字的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很抱歉,如果这个问题可恶的简单,但我正在寻找一种方法来将一列连续整数添加到数据框中(如果我的数据框有200个观察值,例如从第一个1开始观察,最后以200结尾)。



我该怎么做? 对于一个dataframe( df)您可以使用

  df $观察<  -  1:nrow(df)

但是如果你有一个矩阵,你更愿意使用

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

因为使用第一个选项会将您的数据转换为列表。

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


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?

解决方案

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

这篇关于在R中添加连续数字的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆