carlos valiente
carlos valiente

Reputation: 61

R - Is there a way to set dataframe column order based on another dataframe that has different columns

I want to set df2 columns order based on df1 column order. We should take care of: * New columns in df2 that not exists in df1: that columns should be placed as last columns. * Columns in df1 that not exists in df2: this should be created and filled with NAs or empty columns.

Reproducible example:

df1 <- data.frame(x1=1:4, x2=c('a','b', NA, 'd'),x4=4:7, stringsAsFactors=FALSE)
df1[,(ncol(df1)+1):5] <- NA
df1 <- cbind(df1, x3=c(0,0,2,2))
df2 <- data.frame(x3=6:7, x2=c("zz", "qq"),x1=2:3, x66 = 66:67, x77 = 77:78, stringsAsFactors=FALSE)

Expected columns order: "x1" "x2" "x4" "V4" "V5" "x3" "x66" "x77"

Upvotes: 2

Views: 95

Answers (2)

Cole
Cole

Reputation: 11255

Here's a base solution:

df2[, setdiff(names(df1), names(df2))] <- NA
df2[, union(names(df1), names(df2))]

  x1 x2 x4 V4 V5 x3 x66 x77
1  2 zz NA NA NA  6  66  77
2  3 qq NA NA NA  7  67  78

The first command adds new columns based on the names present in df1 but not in df2. The second command then reorders based on what you want.

Upvotes: 0

Shree
Shree

Reputation: 11140

Here's a simple way using dplyr::bind_rows -

bind_rows(df1[NULL, ], df2)

  x1 x2 x4 V4 V5 x3 x66 x77
1  2 zz NA NA NA  6  66  77
2  3 qq NA NA NA  7  67  78

Upvotes: 2

Related Questions