[R] how to combine unequal rows and columns in R
arun
smartpink111 at yahoo.com
Thu Jan 24 17:25:06 CET 2013
HI,
If you use ?join(), it will preserve the order of the first dataframe.
library(plyr)
df3<-rbind.fill(crosspries[[1]],crosspries[[2]])
template2<-read.table(text=outer(unique(df3[,1]),unique(df3[,2]),FUN=paste),sep="",stringsAsFactors=F)
names(template2)<-names(df3[1:2])
res1<-join(template2,df3,by=c("Product","Year_Month"),type="full")
res1
# Product Year_Month prod1 prod2
#1 A 201208 1 NA
#2 B 201208 2 NA
#3 C 201208 1 NA
#4 D 201208 NA NA
#5 F 201208 NA NA
#6 A 201209 1 1
#7 B 201209 2 2
#8 C 201209 NA NA
#9 D 201209 1 1
#10 F 201209 2 1
A.K.
----- Original Message -----
From: Tammy Ma <metal_licaling at live.com>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc:
Sent: Thursday, January 24, 2013 6:25 AM
Subject: [R] how to combine unequal rows and columns in R
HI,
I have the following list:
crosspries
$crosspries[[1]]
Product Year_Month prod1
A 201208 1
B 201208 2
C 201208 1
$crosspries[[2]]
Product Year_Month prod1 prod2
A 201209 1 1
B 201209 2 2
D 201209 1 1
F 201209 2 1
I want to get the following dataframe:
Product Year_Month prod1 prod2
A 201208 1 NA
B 201208 2 NA
C 201208 1 NA
D 201208 NA NA
F 201208 NA NA
A 201209 1 1
B 201209 2 2
C 201209 NA NA
D 201209 1 1
F 201209 2 1
How can I get it in r?
Thanks.
Kind regards,
Tammy
[[alternative HTML version deleted]]
______________________________________________
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.
More information about the R-help
mailing list