How to make a dataframe column of a dataframe outside?

outer_df <- data.frame(x = 1:3, y = letters[1:3])
inner_df <- data.frame(a = c("abc", "bca", "cab"), b = c("little", "some", "more") )
outer_df$sum <- inner_df

expected_df <- data.frame(x = 1:3, y = letters[1:3], sum_a = c("abc", "bca", "cab"), sum_b = c("little", "some", "more") )

Is there an elegant way to do this?

If you can live with sum.a and sum.b rather than sum_a and sum_b, that would do the job, but I would rather use cbind:

## your data 
outer_df <- data.frame(x = 1:3, y = letters[1:3])
inner_df <- data.frame(a = c("abc", "bca", "cab"), b = c("little", "some", "more") )
## new names for the variables, if you want to change them:
names(inner_df) <- paste('sum_',names(inner_df), sep = '')
## joint df
new_df <- cbind(outer_df, inner_df)
## x y sum_a  sum_b
## 1 a   abc little
## 2 b   bca   some
## 3 c   cab   more

cheers

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.