简体   繁体   中英

Assigning column in a df based on a condition from a different df

Probably a simple problem, but I am struggling to get my head around it as a relative beginner. I have two dfs, of which df1 and df 2 are subsets:

#df1
ID1 = c("1","2","3","4","5") 
DisDate1 = c("2010-05-11","2010-07-16","2010-10-25","2010-12-04","2011-03-18") 
ReAdLim1 = c("2010-06-10","2010-08-15","2010-10-24","2011-01-03","2011-04-17") 
df1 = data.frame(ID1,DisDate1,ReAdLim1) 

#df2
ID2 = c("1","1","1","2","2","2") 
DisDate2 = c("2010-09-19","2010-11-16","2011-04-17","2011-06-04","2011-10-11","2011-12-04") 
df2 = data.frame(ID2,DisDate2) 

I simply want to create an additional column in df2 where the ReAdLim1 date from df1 is assigned to each unique ID to create what is depicted by df3:

#df3 
ID3 = c("1","1","1","2","2","2") 
DisDate3 = c("2010-09-19","2010-11-16","2011-04-17","2011-06-04","2011-10-11","2011-12-04")
ReAdLim3 = c("2010-06-10","2010-06-10","2010-06-10","2010-08-15","2010-08-15","2010-08-15") 
df3 = data.frame(ID3,DisDate3,ReAdLim3)

Any suggestions would be greatly appreciated.

这是您要找的东西吗?

merge(df2, df1, by.x = 'ID2', by.y = 'ID1')

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