Add an index (numeric ID) column to large data frame

add-semi-colons :

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

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.

MrFlick :

You can add a sequence of numbers very easily with

data$ID <- seq.int(nrow(data))

If you are already using library(tidyverse), you can use

data <- tibble::rowid_to_column(data, "ID")

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Is there an R function for adding an index (numeric or character ID) column to large data frame by position of groups of values in blocks?

Get column index from data frame that matches numeric vector?

Add a column to a data frame that index the number of occurrences in a group

R add index column to data frame based on row values

How add a running Id new column to Spark Data frame ( pyspark)

Converting a column string to numeric in an r data frame

Converting data frame column from character to numeric

How to convert a data frame column to numeric type?

How to convert a column in a data frame to numeric

Convert data. frame column character to numeric

How to add column to a data frame?

ID column panel data frame

R: Combine list of data frames into single data frame, add column with list index

How to add column with numeric range of ID's after grouping data in Excel

Flatten list column in data frame with ID column

Add leading 0 to column of data in data frame

Add a column to data frame as a list of data frames

Add a column to one data frame based on multiple values in another by index number

Using column values of a data frame to index rows of a multiindex data frame

Select column and row values with an index in data frame

Adding a column based on index to a data frame in Pandas

compare index and column in data frame with dictionary

Identifying Column With Highest Numeric Value in Data Frame in R

How to convert data.frame column from Factor to numeric

Continue numeric series in data.frame column with sequential numbers

R: Convert characters to numeric in data.frame with unknown column classes

R: split string into numeric and return the mean as a new column in a data frame

select numeric columns and one column specified by name from data frame

R Data Frame - Convert words in mostly numeric column to zero