0

I have been trying to find a simpler solution to my problem. I have a data set containing many columns:

S.No    A   B   C   D   E   F........N
 1      2   3   2   5   7   9........20
 2      4   3   5   4   4   5........3  
 .
 .
 .
 .
 3000   4   5   6  3  5  3  6 .........2 

I would like to replace the df$C and df$D columns based on the df$A and df$B column values (ordered pairs).

I have this relation in a smaller data frame (df2) where Z and Q correspond to A and B of df. I need to populate C and D values of df based on R and S values of df2.

 Z   Q    R     S
 1   2   0.3    4
 1   3  -0.3   -4 
 2   2  -0.2   -5
 2   3  -0.5   -1
 .
 .
 .
 .

Currently, I am using the brute force for and if loop wherein I am comparing the values from every row in df for columns df$A and df$B with df2$Z and df2$Q and then populating C and D columns of df based on R and S values in df2. Since I have more than a million values, the loops seems to run forever.

Is there a smarter way to do the same ?

jaycee4u
  • 33
  • 5
  • 2
    `dplyr::left_join(df, df2, by = c("A"="Z", "B"="Q"))` might work. If you want more substantive help, I suggest (a) providing a reproducible question with a small and representative dataset that merges (without giving us something too large); and (b) please "accept" answers for your past questions, even if it's your own answer. It not only is nice "netiquette" and provides "reputation" to the answerers, it also marks the question as resolved (otherwise one might infer the solutions were insufficient ... comments are not always read). – r2evans Jan 30 '17 at 06:42
  • @r2evans: Thanks for your answer ! I have always wanted to rate/ accept answers. But is there something that does not allow users below certain ratings not to accept or rate ? Kindly let me know so that I could do the same for other posts too. I do not see an 'accept answer' link anywhere. Sorry for being so naive !! – jaycee4u Jan 30 '17 at 08:30
  • Look for a check mark to the left of an answer (not on this question, it's been closed as a dup). You can choose one of the offered answers as the one "accepted", and it will be denoted with a green check mark. Note that the one to be "accepted" may be your own, as is the case in one of your questions when the only answer is your own. – r2evans Jan 30 '17 at 08:35
  • @r2evans Thanks once again ! Noted and done ! – jaycee4u Jan 30 '17 at 08:40

0 Answers0