[R] the "union" of several data frame rows

Scot W. McNary smcnary at charm.net
Fri Feb 1 19:58:27 CET 2008


Hi,

I have a question about how to obtain the union of several data frame 
rows.  I'm trying to create a common key for several tests composed of 
different items.   Here is a small scale version of the problem.  These 
are keys for 4 different tests, not all mutually exclusive:

id q1 q2 q3 q4 q5 q6
1  A  C             
2              B  D
3  A     D  B     
4     C  D     B  D

I would like to create a single key all test versions, the "union" of 
the above:

id   q1 q2 q3 q4 q5 q6
key  A  C  D  B  B  D


Here is what I have (unsuccessfully) tried so far:

 > key <-
+   matrix(c("1", "A", "C", " ", " ", " ", " ",
+          "2", " ", " ", " ", " ", "B", "D",
+          "3", "A", " ", "D", "B", " ", " ",
+          "4", " ", "C", "D", " ", "B", "D"),
+        byrow=TRUE, ncol = 7)
 >       
 > k1 <- key[1, 2:7]
 > k2 <- key[2, 2:7]
 > k3 <- key[3, 2:7]
 > k4 <- key[4, 2:7]      
 >
 > itemid <- c("q1", "q2", "q3", "q4", "q5", "q6")
 >
 > k1 <- cbind(itemid, k1)
 > k2 <- cbind(itemid, k2)
 > k3 <- cbind(itemid, k3)
 > k4 <- cbind(itemid, k4)
 >
 > tmp <- merge(k1, k2, by = "itemid")
 > tmp <- merge(tmp, k3, by = "itemid")
 > tmp <- merge(tmp, k4, by = "itemid")
 >
 > t(tmp)
       [,1] [,2] [,3] [,4] [,5] [,6]
itemid "q1" "q2" "q3" "q4" "q5" "q6"
k1     "A"  "C"  " "  " "  " "  " "
k2     " "  " "  " "  " "  "B"  "D"
k3     "A"  " "  "D"  "B"  " "  " "
k4     " "  "C"  "D"  " "  "B"  "D"

The actual problem involves 300 or so items instead of 6 and 10 
different keys instead of four.  Any suggestions welcome.

Thanks in advance,

Scot McNary

 > version
               _                          
platform       i386-pc-mingw32            
arch           i386                       
os             mingw32                    
system         i386, mingw32              
status                                    
major          2                          
minor          6.1                        
year           2007                       
month          11                         
day            26                         
svn rev        43537                      
language       R                          
version.string R version 2.6.1 (2007-11-26)


-- 
Scot McNary
smcnary at charm dot net



More information about the R-help mailing list