[R] Merging two data frames with different columns names

Pete Brecknock Peter.Brecknock at bp.com
Sat Apr 14 22:26:13 CEST 2012


Peterso wrote
> 
> Uwe:
> 
> I was actually trying to stack one table on top of the other. All column
> names are the same except for the Part1 and Part 2. My final table should
> look like the table below. Maybe it is possible to change  the names of
> Part1 and Part 2 to Part?
> 
> A B C Part
> 1 0 1 550
> 0 1 1 669
> etc....
> 

A couple of approaches using the rbind function ...

require(conf.design)

# Your Data
d1 <- conf.design(c(1,1,1), p=2, block.name="blk", treatment.names =
c("A","B","C"))
d2 <- conf.design(c(1,1,1), p=2, block.name="blk", treatment.names =
c("A","B","C"))

rep1 <- c(550,669,633,642,1037,749,1075,729)
rep2 <- c(604,650,601,635,1052,868,1063,860)


# Approach 1
part1 <- data.frame(d1,part=rep1)
part2 <- data.frame(d2,part=rep2)
d12 <- rbind(part1,part2) 

# Approach 2
part1 <- data.frame(d1,rep1)
names(part1)<- c("blk","A","B","C","part")
part2 <- data.frame(d2,rep2)
names(part2)<- c("blk","A","B","C","part")
d12 <- rbind(part1, part2)

HTH

Pete

--
View this message in context: http://r.789695.n4.nabble.com/Merging-two-data-frames-with-different-columns-names-tp4556400p4557974.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list