简体   繁体   中英

fill a column based on another one in R

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?

我们可以使用match

df1$col2 <- match(df1$col1, unique(df1$col1))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM