I have a large dataframe (50+ columns). I get each row in different order of columns and I need to add a new row to the dataframe but this new row should update the right columns.
For example, I have a df
:
col1 col2 col3 col4 col5 col6 col7 col8 col9 col10 .........
1 a Don Lu 854 W eee 1 1234 yes
4 s34 Dino Ken 44 S aaa 1 3432 no
5 1ee Pupu Dar 2215 R bbb -4 12121 yes
I get a new entry:
col6 col2 col5 col4 col3 col1 col7 col10 col9 col8 .........
R re3 666 Rino Pino 33 ddd no 55874 6
I thought about dplyr
arrange function to arrange
by names and update,
I can also use $
to update each one, please advise what is the best practice?
Asumming your new entry is a data.frame()
and called df1
, I would use rbindlist()
from the data.table
package.
merged <- list(df, df1)
rbindlist(merged, fill = TRUE)
will do the job very quickly. In fact, you can put as many entries you want in the list
merged <- list(df1, df2, ...... , df99999, df100000)
rbindlist(merged, fill = TRUE)
does the job pretty quickly.
Note that any entries in df1
that are not in df
and vise-versa will be made NA
accordingly.