'change values of column to consecutive integers

I have a CSV where the first column is my subject ID number, there's a total of 311 subjects, and on average 1000 values per subject. The subject ID numbers are quite random (although integers only) ranging from 72 to 2265988. What I would like to do is neatly rename them to numbers 1-311.

What would be the quickest way to do this in R, preferably, or in excel?



Solution 1:[1]

OK. Here is your solution. You just replace with your names:

df <- data.frame(ID = c(1,3,5,3,6,7),
             var = c(3,6,8,5,7,8))             

temp <- data.frame(old_id = sort(unique(df$ID)),
              new_id =seq(1,5))

replace_ID <-  temp$new_id[match(df$ID, temp$old_id)]
df$ID <- replace_ID
df

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bloxx