在大数据框中添加索引(数字ID)列 [英] Add an index (numeric ID) column to large data frame
问题描述
我将一个大的csv文件读入了数据帧。 csv文件中的数据来自代表用户信息的多个网站。例如,这里是数据帧的结构。
I have a read large csv file into a data frame. Data in the csv file are from multiple web sites representing user information. For example here is the structure of the data frame.
user_id, number_of_logins, number_of_images, web
001, 34, 3, aa.com
002, 4, 4, aa.com
034, 3, 3, aa.com
001, 12, 4, bb.com
002, 1, 3, bb.com
034, 2, 2, cc.com
您可以看到将数据放入数据框后,user_id不再是唯一ID,这将导致所有分析。我正在尝试在 user_id
之前添加另一列,类似于 generated_uid
,并且几乎使用了该列要填充的 data.frame
。做到这一点的最佳方法是什么。
as you can see once I bring the data into the data frame user_id is no longer a unique id and this causes all the analysis. I am trying to add another columns prior to user_id
which is something like "generated_uid"
and pretty much use the index of the data.frame
to be filled by that column. What's the best way to accomplish this.
推荐答案
您可以使用
data$ID <- seq.int(nrow(data))
如果您已经在使用 library(tidyverse)
,您可以使用
If you are already using library(tidyverse)
, you can use
data <- tibble::rowid_to_column(data, "ID")
这篇关于在大数据框中添加索引(数字ID)列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!