Search code examples
ralphanumeric

alphanumeric to increasing numbers in R


I have a table in R with a column of ID's like: '1A', '2A', '1B', '2B', etc.

I would like to convert them to numbers such as: 1, 2, 3, 4, etc.

I would appreciate any suggestion. Thanks!


Solution

  • Another way is to convert to a factor, and then to numbers. that way there is no assumptions of sequential order.

    df <- data.frame(list(
      IDs=c('1A', '2A', '1B', '2B'),
      vals=c(8,8,8,8)
    ))
    
    df$IDs <- as.numeric(as.factor(df$IDs))