0

I have the following 2 dataframes.

My first DF is this one, I such a DDBB, where I have only one occurrence from each ISIN value.

 ISIN inst inst_type type
    asd    1    2     ETF
    asb    2    b     Bond
    bcd    3    c     Bond

My second DF is the following one,

    ISIN inst inst_type type
    asd    1      2      ""
    asd    1      2      ""
    bcd    3      c      ""
    bcd    3      c      ""
    bcd    3      c      ""
    asb    2      b      ""

What I want to do is to fill second dataframe 'type' column base on the first DF , for example. In my first DF for ISIN 'asd' type is 'ETF' so my second ETF should be like this.

 ISIN inst inst_type type
    asd    1      2      ETF
    asd    1      2      ETF
    bcd    3      c      ""
    bcd    3      c      ""
    bcd    3      c      ""
    asb    2      b      ""

I can do it using for loop but my problem is that if I have a Df with 400k rows it is not a efficient way for doing that.

Any idea how to solve it more efficient?

fiticida
  • 664
  • 1
  • 10
  • 24
  • `merge(DF2[-4], DF1[-(2:3)], all.x=TRUE)` related: https://stackoverflow.com/questions/1299871/how-to-join-merge-data-frames-inner-outer-left-right – jogo Oct 25 '18 at 14:17

1 Answers1

0

You can use a for loop but looping in the differents ISIN and not in the rows, it would be more efficient. like this:

factors <- unique(df1$ISIN)
for(i in factors){
  df2$type[df2$ISIN==i] <- df1$type[df1$ISIN==i][1]
}
Santiago I. Hurtado
  • 1,113
  • 1
  • 10
  • 23