Simplest Way to Get Rbind to Ignore Column Names

Simplest way to get rbind to ignore column names

You might find setNames handy here...

rbind(df, setNames(rev(df), names(df)))
# x y
#1 1 3
#2 2 4
#3 3 1
#4 4 2

I suspect your real use-case is somewhat more complex. You can of course reorder columns in the first argument of setNames as you wish, just use names(df) in the second argument, so that the names of the reordered columns match the original.

rbind dataframes with a different column name

You could use rbindlist which takes different column names. Using @LyzandeR's data

library(data.table) #data.table_1.9.5
rbindlist(list(a,b))
# a b
# 1: 0.8403348 0.1579255
# 2: 0.4759767 0.8182902
# 3: 0.8091875 0.1080651
# 4: 0.9846333 0.7035959
# 5: 0.2153991 0.8744136
# 6: 0.7604137 0.9753853
# 7: 0.7553924 0.1210260
# 8: 0.7315970 0.6196829
# 9: 0.5619395 0.1120331
#10: 0.5711995 0.7252631

Update

Based on the object names of the 12 datasets (i.e. 'Goal1_Costo', 'Goal2_Costo',..., 'Goal12_Costo'),

 nm1 <- paste(paste0('Goal', 1:12), 'Costo', sep="_")
#or using `sprintf`
#nm1 <- sprintf('%s%d_%s', 'Goal', 1:12, 'Costo')
rbindlist(mget(nm1))

rbind data.frames without names

Because unname() & explicitly assigning NA as column headers are not identical actions. When the column names are all NA, then an rbind() is possible. Since rbind() takes the names/colnames of the data frame, the results do not match & hence rbind() fails.

Here is some code to help see what I mean:

> c1 <- c(1,2,3)
> c2 <- c('A','B','C')
> df1 <- data.frame(c1,c2)
> df1
c1 c2
1 1 A
2 2 B
3 3 C
> df2 <- data.frame(c1,c2) # df1 & df2 are identical
>
> #Let's perform unname on one data frame &
> #replacement with NA on the other
>
> unname(df1)
NA NA
1 1 A
2 2 B
3 3 C
> tem1 <- names(unname(df1))
> tem1
NULL
>
> #Please note above that the column headers though showing as NA are null
>
> names(df2) <- rep(NA,ncol(df2))
> df2
NA NA
1 1 A
2 2 B
3 3 C
> tem2 <- names(df2)
> tem2
[1] NA NA
>
> #Though unname(df1) & df2 look identical, they aren't
> #Also note difference in tem1 & tem2
>
> identical(unname(df1),df2)
[1] FALSE
>

I hope this helps. The names show up as NA each, but the two operations are different.

Hence, two data frames with their column headers replaced to NA can be "rbound" but two data frames without any column headers (achieved using unname()) cannot.

How to rbind different data frames with different column names?

Simply use:

colnames(E)=colnames(N1)=colnames(N2)

D <- rbind(E, N1, N2)

Remember that for the rbind to work the dataframes should have the same number of columns.

how to change column name in several data frame in the same time?

You can do it using magrittr and dplyr :

d1 <- mtcars
d2 <- d1
d3 <- d1

names(d2) <- paste0(names(d2), "_2")
names(d3) <- paste0(names(d2), "_3")

rbind(d1, d2, d3) # gives an error, ok
#> Error in match.names(clabs, names(xi)): les noms ne correspondent pas aux noms précédents

library(magrittr, quietly = TRUE, warn.conflicts = FALSE)
library(dplyr, quietly = TRUE, warn.conflicts = FALSE)

df_list <- list(d2, d3)
df_list <- lapply(df_list, magrittr::set_colnames, names(d1))

df_final <- rbind(d1, dplyr::bind_rows(df_list) )
nrow(df_final) == 3* nrow(d1)
#> [1] TRUE

rbind based on columns name and exclude no match

Here's one way to do it:

match_all_columns <- function (d, col) {
if (all(names(d) %in% col)) {
out <- d[, col]
} else {
out <- NULL
}
out
}
# or as a one-liner
match_all_columns <- function (d, col) if (all(names(d) %in% col)) d[col]

matched_data <- lapply(l, match_all_columns, col)
result <- do.call(rbind, matched_data)
result
# X1 X2 X3 X4 X5
# x 1 2 3 4 5
# z 11 12 13 14 15

rbind knows to just ignore the NULL elements.

edit: I swapped d[, col] with d[col] because a) it looks nicer, b) it prevents the data frame being dropped to a vector if col only has one element, and c) I think it's slightly more performant on large data frames.

Extract column names from df as a vector - or copy / paste column names to new df?

In one line:

df3 <- rbind(df1, setNames(df2, names(df1)))


Related Topics



Leave a reply



Submit