Search code examples
rdata.tabledrop

Dropping the last two numbers from every entry in a column of data.table


Preface: I am a beginner to R that is eager to learn. Please don't mistake the simplicity of the question (if it is a simple answer) for lack of research or effort!

Here is a look at the data I am working with:

         year state age  POP
     1:   90  1001   0  239
     2:   90  1001   0  203
     3:   90  1001   1  821
     4:   90  1001   1  769
     5:   90  1001   2 1089

The state column contains the FIPS codes for all states. For the purpose of merging, I need the state column to match my another dataset. To achieve this task, all I have to do is omit the last two numbers for each FIPS code such that the table looks like this:

         year state age  POP
     1:   90  10     0  239
     2:   90  10     0  203
     3:   90  10     1  821
     4:   90  10     1  769
     5:   90  10     2 1089

I can't figure out how to accomplish this task on a numeric column. Substr() makes this easy on a character column.


Solution

  • In case your number is not always 4 digits long, to omit the last two you can make use of the vectorized behavior of substr()

    x <- rownames(mtcars)[1:5]
    x
    #> [1] "Mazda RX4"         "Mazda RX4 Wag"     "Datsun 710"       
    #> [4] "Hornet 4 Drive"    "Hornet Sportabout"
    substr(x, 1, nchar(x)-2)
    #> [1] "Mazda R"         "Mazda RX4 W"     "Datsun 7"        "Hornet 4 Dri"   
    #> [5] "Hornet Sportabo"
    
    # dummy code for inside a data.table
    dt[, x_new := substr(x, 1, nchar(x)-2)]