0

I have a column named quadrant in my dataset:

Quadrant
NW
NE
SW
NE

I want to create a new column named Quad_id containing custom ids for each direction like:

Quadrant      Quad_id
NW            10001 
NE            10002
SW            10003
NE            10002

1 Answers1

0

There are two possibilities:

  • You can use match:

    transform(dat, id = match(Quadrant, Quadrant) + 10000)
    #   Quadrant    id
    # 1       NW 10001
    # 2       NE 10002
    # 3       SW 10003
    # 4       NE 10002
    

    where dat is the name of the data frame.

  • You can also use as.factor:

    transform(dat, id = as.integer(as.factor(Quadrant)) + 10000)
    #   Quadrant    id
    # 1       NW 10002
    # 2       NE 10001
    # 3       SW 10003
    # 4       NE 10001
    
Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168