Dear R-help, I wonder if you could give me some suggestions in how to do a union join of two data frames as follow: -> union join the common column, and insert a 0 if one is missing. I made a function to perform the following, and I know it may not that quite welly written, but it works. Any suggestions are welcome, many thanks. Anthony> q1 = data.frame(a=1,b=2,c=3,row.names="q1")a b c q1 1 2 3> q2 = data.frame(d=4,b=1,a=4, row.names="q2")d b a q2 4 1 4 -> myJoinColumns(q1,q2) a b c d q1 1 2 3 0 q2 4 1 0 4 myJoinColumns <- function(q1,q2){ allNames = sort(union(colnames(q1),colnames(q2))) for (i in 1:length(allNames)){ t1 = which(colnames(q1) == allNames[i]) t2 = which(colnames(q2) == allNames[i]) if (length(t1) == 1){ sec1 = q1[,t1] } else { sec1 = 0 } if (length(t2) == 1){ sec2 = q2[,t2] } else { sec2 = 0 } if (i == 1){ qTable = matrix(c(sec1,sec2)) }else{ qTable = cbind(qTable,c(sec1,sec2)) } } colnames(qTable) = allNames rownames(qTable) = c("q1","q2") qTable }
On Aug 10, 2011, at 11:04 AM, Anthony Ching Ho Ng wrote:> Dear R-help, > > I wonder if you could give me some suggestions in how to do a union > join of two data frames as follow: > -> union join the common column, and insert a 0 if one is missing. > > I made a function to perform the following, and I know it may not that > quite welly written, but it works. > > Any suggestions are welcome, many thanks. > > Anthony > >> q1 = data.frame(a=1,b=2,c=3,row.names="q1") > a b c > q1 1 2 3 > >> q2 = data.frame(d=4,b=1,a=4, row.names="q2") > d b a > q2 4 1 4 > > -> myJoinColumns(q1,q2) > a b c d > q1 1 2 3 0 > q2 4 1 0 4> temp <- merge(q1,q2, all=TRUE) > temp[is.na(temp)] <- 0 > temp a b c d 1 1 2 3 0 2 4 1 0 4> > > myJoinColumns <- function(q1,q2){ > allNames = sort(union(colnames(q1),colnames(q2))) > for (i in 1:length(allNames)){ > t1 = which(colnames(q1) == allNames[i]) > t2 = which(colnames(q2) == allNames[i]) > > if (length(t1) == 1){ > sec1 = q1[,t1] > } else { > sec1 = 0 > } > > if (length(t2) == 1){ > sec2 = q2[,t2] > } else { > sec2 = 0 > } > > if (i == 1){ > qTable = matrix(c(sec1,sec2)) > }else{ > qTable = cbind(qTable,c(sec1,sec2)) > } > } > colnames(qTable) = allNames > rownames(qTable) = c("q1","q2") > qTable > } > > ______________________________________________ > 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.David Winsemius, MD West Hartford, CT
Try this: merge(q1, q2, all = TRUE) On Wed, Aug 10, 2011 at 12:04 PM, Anthony Ching Ho Ng <anthony.ch.ng at gmail.com> wrote:> Dear R-help, > > I wonder if you could give me some suggestions in how to do a union > join of two data frames as follow: > -> union join the common column, and insert a 0 if one is missing. > > I made a function to perform the following, and I know it may not that > quite welly written, but it works. > > Any suggestions are welcome, many thanks. > > Anthony > >> q1 = data.frame(a=1,b=2,c=3,row.names="q1") > ? ? a b c > q1 1 2 3 > >> q2 = data.frame(d=4,b=1,a=4, row.names="q2") > ? ? d b a > q2 4 1 4 > > -> ?myJoinColumns(q1,q2) > ? ? a b c d > q1 1 2 3 0 > q2 4 1 0 4 > > > myJoinColumns <- function(q1,q2){ > ?allNames = sort(union(colnames(q1),colnames(q2))) > ?for (i in 1:length(allNames)){ > ? ?t1 = which(colnames(q1) == allNames[i]) > ? ?t2 = which(colnames(q2) == allNames[i]) > > ? ?if (length(t1) == 1){ > ? ? ?sec1 = q1[,t1] > ? ?} else { > ? ? ?sec1 = 0 > ? ?} > > ? ?if (length(t2) == 1){ > ? ? ?sec2 = q2[,t2] > ? ?} else { > ? ? ?sec2 = 0 > ? ?} > > ? ?if (i == 1){ > ? ? ?qTable = matrix(c(sec1,sec2)) > ? ?}else{ > ? ? ?qTable = cbind(qTable,c(sec1,sec2)) > ? ?} > ?} > ?colnames(qTable) = allNames > ?rownames(qTable) = c("q1","q2") > ?qTable > } > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Seemingly Similar Threads
- incorrect number of subscriptions error in user-written function
- Samba ignores write and read list
- creating a factor from other factors and ifelse
- RJS - replacing html for multiple entries
- [EXTERNAL] Re: NOTE: multiple local function definitions for ?fun? with different formal arguments