An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120509/6f944472/attachment.pl>
Combine two tables by row with different columns
5 messages · David Winsemius, Rui Barradas, Kristi Glover +1 more
On May 9, 2012, at 9:21 PM, Kristi Glover wrote:
Hi R user, I could not combine two tables. Would any one help me on how I can combine with following example tables?
You do realize that tables in R are not dataframes. They are a type of matrix.
T1
X Y Z XX A 1 5 9 13 B 2 6 10 14 C 3 7 11 15 D 4 8 12 16
T2
X Y XX a 1 4 7 b 2 5 8 c 3 6 9 I want to get the following table cT X Y XX A 1 5 13 B 2 6 14 C 3 7 15 D 4 8 16 A1 1 4 7 B1 2 5 8 C1 3 6 9
Assuming you will accept the rownames as they stand, then try this: cT <- cbind(T1,T2) # works for table, matrices, and dataframes
[[alternative HTML version deleted]]
Plain text, please.
David Winsemius, MD West Hartford, CT
Hello, Try this T1 <- read.table(text=" X Y Z XX A 1 5 9 13 B 2 6 10 14 C 3 7 11 15 D 4 8 12 16 ", header=TRUE) T2 <- read.table(text=" X Y XX a 1 4 7 b 2 5 8 c 3 6 9 ", header=TRUE) cT <- read.table(text=" X Y XX A 1 5 13 B 2 6 14 C 3 7 15 D 4 8 16 A1 1 4 7 B1 2 5 8 C1 3 6 9 ", header=TRUE) T1; T2; cT (Test <- rbind(T1[, colnames(T2)], T2)) # rownames are different, 'Test' has rownames from T2 all.equal(cT, Test) Hope this helps, Rui Barradas Kristi Glover wrote
Hi R user, I could not combine two tables. Would any one help me on how I can combine with following example tables?
T1
X Y Z XX A 1 5 9 13 B 2 6 10 14 C 3 7 11 15 D 4 8 12 16
T2
X Y XX a 1 4 7 b 2 5 8 c 3 6 9 I want to get the following table cT X Y XX A 1 5 13 B 2 6 14 C 3 7 15 D 4 8 16 A1 1 4 7 B1 2 5 8 C1 3 6 9 Thanks for your help. Thanks, Kristi === [[alternative HTML version deleted]]
______________________________________________ R-help@ 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.
-- View this message in context: http://r.789695.n4.nabble.com/Combine-two-tables-by-row-with-different-columns-tp4622276p4622321.html Sent from the R help mailing list archive at Nabble.com.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120509/99ece04c/attachment.pl>
Just omit "Z" when you combine T1 and T2 -- incidentally, dput() is an
even easier way to make a reproducible example (and you don't need
rep() for your rownames since you arent repeating anything)
T1 <- structure(1:16, .Dim = c(4L, 4L), .Dimnames = list(c("A", "B",
"C", "D"), c("X", "Y", "Z", "XX")))
T2 <- structure(1:9, .Dim = c(3L, 3L), .Dimnames = list(c("A1", "B1",
"C1"), c("X", "Y", "XX")))
It sounds like you want to get the intersection of the colnames for
your combined object so something like:
cn <- intersect(colnames(T1), colnames(T2))
rbind(T1[, cn], T2[, cn])
# I think David misread -- it sounds like you want rbind here
Hope this helps,
Michael
On Wed, May 9, 2012 at 10:04 PM, Kristi Glover
<kristi.glover at hotmail.com> wrote:
Thanks David, I tried 'cbind" as you suggested: but did not work. Here are the example
T1=matrix(1:16, ncol=4)
rownames(T1)=rep(c("A","B","C","D"))
colnames(T1)=rep(c("X","Y","Z","XX"))
T2=matrix(1:9, ncol=3)
rownames(T2)=rep(c("A1","B1","C1"))
colnames(T2)=rep(c("X","Y","XX"))
T1
?X Y ?Z XX A 1 5 ?9 13 B 2 6 10 14 C 3 7 11 15 D 4 8 12 16
T2
? X Y XX A1 1 4 ?7 B1 2 5 ?8 C1 3 6 ?9
T1T2<-cbind(T1,T2)
Error in cbind(T1, T2) : ?number of rows of matrices must match (see arg 2) I want to have the following table (probably you noticed that column Z in Table 1 is not in Table 2) X Y ?XX A 1 5 13 B 2 6 14 C 3 7 15 D 4 8 16 A1 1 4 ?7 B1 2 5 ?8 C1 3 6 ?9 any suggestions? this is just an example, I have very big files (~1700 columns, and ~2500 rows) Kristi
CC: r-help at r-project.org From: dwinsemius at comcast.net To: kristi.glover at hotmail.com Subject: Re: [R] Combine two tables by row with different columns Date: Wed, 9 May 2012 21:47:48 -0400 On May 9, 2012, at 9:21 PM, Kristi Glover wrote:
Hi R user, I could not combine two tables. Would any one help me on how I can combine with following example tables?
You do realize that tables in R are not dataframes. They are a type of matrix.
T1
?X Y ?Z XX A 1 5 ?9 13 B 2 6 10 14 C 3 7 11 15 D 4 8 12 16
T2
? X Y XX a 1 4 ?7 b 2 5 ?8 c 3 6 ?9 I want to get the following table cT ? X Y XX A 1 5 ?13 B 2 6 ?14 C 3 7 15 D 4 8 16 A1 1 4 ?7 B1 2 5 ?8 C1 3 6 ?9
Assuming you will accept the rownames as they stand, then try this: cT <- cbind(T1,T2) ? # works for table, matrices, and dataframes
? ? [[alternative HTML version deleted]]
Plain text, please. -- David Winsemius, MD West Hartford, CT
? ? ? ?[[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.