Hi,
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))
I think I already did a similar procedure sometime ago with the
cast() command from reshape-package but I cant remember correctly...
...maybe someone can point me to the correct formula...
Best
/johannes
Hi Johannes,
Your example is tricky because the original df1 does not contain id
columns that identify each cell in df2. If you want to use the
reshape2 package for this I think you have to add a second id
variable:
df1 <- data.frame(col1 =
c("A","A","B","B"),col2 = c(1:4), col3 =
c(1:4))
library(reshape2)
library(plyr)
df1 <- ddply(df1,
"col1",
transform,
col0 = 1:length(col1))
tmp <- melt(df1, id.vars = c("col0","col1"))
dcast(tmp, col0 ~ col1 + variable)
Best,
Ista
On Fri, Jan 11, 2013 at 7:46 AM, Johannes Radinger
<johannesradinger at gmail.com> wrote:> Hi,
>
> 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))
>
> I think I already did a similar procedure sometime ago with the
> cast() command from reshape-package but I cant remember correctly...
> ...maybe someone can point me to the correct formula...
>
> Best
>
> /johannes
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
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)) > df1col1 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)) > df2A.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]]
HI,
May be this also works for you:
?do.call(cbind,by(df1[,-1],df1[,1],function(x) x))
?# A.col2 A.col3 B.col2 B.col3
#1????? 1????? 1????? 3????? 3
#2????? 2????? 2????? 4????? 4
A.K.
----- Original Message -----
From: Johannes Radinger <johannesradinger at gmail.com>
To: r-help at r-project.org
Cc:
Sent: Friday, January 11, 2013 7:46 AM
Subject: [R] split & rbind (cast) dataframe
Hi,
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))
I think I already did a similar procedure sometime ago with the
cast() command from reshape-package but I cant remember correctly...
...maybe someone can point me to the correct formula...
Best
/johannes
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.