Combine two data frames by rows (rbind) when they have different sets of columns
rbind.fill
from the package plyr
might be what you are looking for.
Combining data frames with unequal number of columns
There's also rbind.fill
from plyr
or Stack
.
library(plyr)
rbind.fill(Dat1, Dat2)
## Col1 Col2 Col3 Col4 Col5
## 1 A1 56 89 NA <NA>
## 2 A2 49 NA 84 F11
library(Stack)
Stack(Dat1, Dat2)
## Col1 Col2 Col3 Col4 Col5
## 1 A1 56 89 NA <NA>
## 2 A2 49 NA 84 F11
Combine two dataframes by row (irrespective of columns names)
A base R option is list2DF
+ Map
> list2DF(Map(c, obj1, obj))
emp degree
1 10 1
2 10 1
3 10 1
4 10 1
5 10 1
6 10 1
7 1990 223
8 1991 224
9 1992 225
10 1993 226
11 1994 227
12 1995 228
Also, you can try rbindlist
from data.table
package
data.table::rbindlist(list(obj1, obj), use.names = FALSE)
which gives
emp degree
1: 10 1
2: 10 1
3: 10 1
4: 10 1
5: 10 1
6: 10 1
7: 1990 223
8: 1991 224
9: 1992 225
10: 1993 226
11: 1994 227
12: 1995 228
cbind a dataframe with an empty dataframe - cbind.fill?
Here's a cbind fill:
cbind.fill <- function(...){
nm <- list(...)
nm <- lapply(nm, as.matrix)
n <- max(sapply(nm, nrow))
do.call(cbind, lapply(nm, function (x)
rbind(x, matrix(, n-nrow(x), ncol(x)))))
}
Let's try it:
x<-matrix(1:10,5,2)
y<-matrix(1:16, 4,4)
z<-matrix(1:12, 2,6)
cbind.fill(x,y)
cbind.fill(x,y,z)
cbind.fill(mtcars, mtcars[1:10,])
I think I stole this from somewhere.
EDIT STOLE FROM HERE: LINK
Related Topics
How to Add Row and Column to a Dataframe of Different Length
R: Error in Usemethod("Tbl_Vars")
Replace Column Values With Na Based on a Different Column or Row Position With Tidyverse
Change R Default Library Path Using .Libpaths in Rprofile.Site Fails to Work
How to Identify/Delete Non-Utf-8 Characters in R
Quickly Reading Very Large Tables as Dataframes
Why Does Summarize or Mutate Not Work With Group_By When I Load 'Plyr' After 'Dplyr'
How to Sort a Character Vector Where Elements Contain Letters and Numbers
In R, How to Get an Object'S Name After It Is Sent to a Function
Cluster Analysis in R: Determine the Optimal Number of Clusters
If Else Statements to Check If a String Contains a Substring in R
Regex to Replace Comma to Dot Separator
Change the Class from Factor to Numeric of Many Columns in a Data Frame
Join 3 Columns of Different Lengths in R
Pass a String as Variable Name in Dplyr::Filter
Faster Ways to Calculate Frequencies and Cast from Long to Wide
What Are the Differences Between "=" and "≪-" Assignment Operators