user3684014
user3684014

Reputation: 1175

How are cbind and rbind implemented?

Suppose I have two data frames df1 and df2, and I want to cbind them:

df1 <- data.frame(a=c(1,2), b=c(3,4))
df2 <- data.frame(c=c(3,4), d=c(5,7))
df1 <- cbind(df1,df2)

When I write the line 3 this way, would R create a new larger data frame and assign it to df1 or it would optimized to add the new columns of df2 into df1 in place to save memory? Are there any documents on this?

Upvotes: 0

Views: 778

Answers (1)

Luke Tierney
Luke Tierney

Reputation: 1055

A data frame is a vector of columns. R will create a new vector for the new df1 result. Earlier versions would also duplicate the columns, but as of 3.1.0 R no longer does this and the columns will be shared between the new df1 and the old df1 and df2.

Upvotes: 1

Related Questions