On Fri, Jan 11, 2013 at 6:46 AM, Johannes Radinger <
johannesradinger@gmail.com> wrote:

> I would like to split dataframe based on one colum and want
> to connect the two dataframes by rows (like rbind). Here a small example:
>
> # The orgininal dataframe
> df1 <- data.frame(col1 = c("A","A","B","B"),col2 = c(1:4), col3 = c(1:4))
>
> # The datafame how it could look like
> df2 <- data.frame(A.col2 = c(1,2), A.col3 = c(1,2), B.col2 = c(3,4),
> B.col3 = c(3,4))
>

Given your example data:

> df1 <- data.frame(col1 = c("A","A","B","B"),col2 = c(1:4), col3 = c(1:4))
> df1
  col1 col2 col3
1    A    1    1
2    A    2    2
3    B    3    3
4    B    4    4
> df2 <- data.frame(A.col2 = c(1,2), A.col3 = c(1,2), B.col2 =
c(3,4), B.col3 = c(3,4))
> df2
  A.col2 A.col3 B.col2 B.col3
1      1      1      3      3
2      2      2      4      4

You might try this:

> x <- split(df1[,-1], df1$col1)
> do.call(cbind, x)
  A.col2 A.col3 B.col2 B.col3
1      1      1      3      3
2      2      2      4      4

That seems to match your intended output.

HTH

James

	[[alternative HTML version deleted]]


