Home > OS >  fill a column based on another one in R
fill a column based on another one in R

Time:07-22

I have the following dt:

col1 col2.
A 1
A 2
B 3

What I want is :

col1 col2.
A 1
A 1
B 2

I tried many things but it didn't work for me? Does anyone have an idea how to do it?

CodePudding user response:

We may use match

df1$col2 <- match(df1$col1, unique(df1$col1))
  •  Tags:  
  • r
  • Related