Skip to contents

Keep rows order in data.frame when a function is applied to it.

Usage

keep_df_order(df, fun, ...)

Arguments

df

the data.frame that will be changed by the function fun

fun

the function that will be applied

...

other parameters to fun

Source of inspiration

A comment from landroni (2017-09-23 at 15:59) on http://stackoverflow.com

Examples


df_1 = data.frame(object = c('A', 'B', 'D', 'F', 'C'), class = c(2, 1, 2, 3, 1))
df_2 = data.frame(class = c(1, 2, 3), prob = c(0.5, 0.7, 0.3))
merge(df_1, df_2)
#>   class object prob
#> 1     1      B  0.5
#> 2     1      C  0.5
#> 3     2      A  0.7
#> 4     2      D  0.7
#> 5     3      F  0.3
keep_df_order(df_1, merge, df_2, suffixes = c("_x","_y"))
#>   class object prob
#> 3     2      A  0.7
#> 1     1      B  0.5
#> 4     2      D  0.7
#> 5     3      F  0.3
#> 2     1      C  0.5