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

From: J. Scott Olsson <olsson_at_math.umd.edu>
Date: Fri, 1 Feb 2008 14:19:44 -0500

It's ugly, but you could use something like

 sum(tmp[i,] == "A") > 0

on each column.

pax,
Scott

On Fri, Feb 1, 2008 at 1:58 PM, Scot W. McNary <smcnary_at_charm.net> wrote:

> 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
>
> ______________________________________________
> 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.
>

        [[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. Received on Fri 01 Feb 2008 - 19:23:49 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 01 Feb 2008 - 20:30:10 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive