Reputation: 739
I have two .rda files with many variables and dataframes already set to some values. Both the rda files have same variable names and dataframe names. However, their order is different. I want to combine two rda files into 1 single .rda file like the way we append two dataframes.
I tried to use rbind
to combine two objects containing .rda files like below
X <- c(1,2,3)
Y <- c("A","B","C")
rbind(X,Y)
save(X,Y,file="samplerda.rda")
load(file = "samplerda.rda")
X <- c(4,5,6)
Y <- c("D","E","F")
save(X,Y,file="samplerda1.rda")
load(file = "samplerda1.rda")
ob1 <- load(file = "samplerda.rda")
ob2 <- load(file = "samplerda1.rda")
combine12 <- rbind(ob1, ob2)
save(combine12, file="En1.rda")
load("En1.rda")
print(combine12)
However I print combine12 object i was expecting below output
X Y
1 "A"
2 "B"
3 "C"
4 "D"
5 "E"
6 "F"
Actual Output is
[,1] [,2]
ob1 "X" "Y"
ob2 "X" "Y"
Upvotes: 1
Views: 996
Reputation: 44987
The load()
function returns the names of the objects it loaded, so both ob1
and ob2
will contain c("X", "Y")
and when you rbind
them, you get the matrix you saw in combine12
.
You'll need much more complicated code to do what you want, for example:
env1 <- new.env()
env2 <- new.env()
ob1 <- load(file = "samplerda.rda", envir = env1)
ob2 <- load(file = "samplerda1.rda", envir = env2)
stopifnot(all(ob1 == ob2)) # Sanity check
combine12 <- list()
for (n in ob1) {
combine12[[n]] <- c(get(n, env1), get(n, env2))
}
combine12 <- as.data.frame(combine12)
Upvotes: 1