简体   繁体   English

R:为大型数据集编号级别并分配给新列

[英]R: Number levels and assign to new column, for a big dataset

So this is a quick question. 所以这是一个快速的问题。
I have a dataframe of panel data, in which I have a column of identifications/names/IDs for each individual. 我有一个面板数据的数据框,其中每个人都有一列标识/名称/ ID。 Lets say there are n levels to this column, that is, n individuals in the panel over a certain timeframe. 可以说此列有n个级别,即在特定时间范围内面板中的n个个体。
I want to add a column N to the dataframe with this value n, that is a numbering of levels. 我想使用此值n向数据框添加列N,即级别的编号。
That is each ID/name/level gets assigned a number from 1 through n. 也就是说,为每个ID /名称/级别分配了一个从1到n的数字。 Here is a code that does what I want: 这是执行我想要的代码:

i = 1
for(l in levels(data$IDs))  {
data[data$ID == l,]$N = i
i = i+ 1
}

So far so good. 到现在为止还挺好。 Issue: My dataset is large. 问题:我的数据集很大。 Very large. 很大。 Far too much to do this manually. 手动执行此操作太多了。 And the above operation takes too much time. 并且上述操作花费太多时间。 This is a loop, so my guess is that there is a faster way to do this in R using vector operations. 这是一个循环,所以我猜测是使用向量运算在R中有一种更快的方法。 Anyone know a computationally quick way to do this? 有人知道一种计算快速的方法吗?

Just use data$N <- as.integer(data$ID) . 只需使用data$N <- as.integer(data$ID) Factor variables are integers internally. 因子变量在内部是整数。 Thus, it is easy to turn them into integer variables. 因此,很容易将它们转换为整数变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM