Re: [R] Matched pairs with two data frames

From: <ukoenig_at_med.uni-marburg.de>
Date: Mon, 21 Apr 2008 07:43:58 +0200

Thankīs a lot, Patrick!
Thatīs what I was looking for...

Zitat von Patrick Connolly <p_connolly_at_slingshot.co.nz>:

> On Wed, 16-Apr-2008 at 11:58AM +0200, Udo wrote:
>
>
> |> I only "need" line 1, 6 and 9. To show this,
> |> I added "needed" by hand.
> |>
> |> age school out1 out2 needed
> |> 1 1 10 9.5 1.1 yes
> |> 2 1 10 9.5 2.0 no
> |> 3 1 10 9.5 3.5 no
> |> 4 1 10 9.5 4.9 no
> |> 5 1 10 2.3 1.1 no
> |> 6 1 10 2.3 2.0 yes
> |> 7 1 10 2.3 3.5 no
> |> 8 1 10 2.3 4.9 no
> |> 9 2 20 3.3 6.5 yes
> |> 10 2 20 4.1 6.5 no
> |> 11 2 20 5.9 6.5 no
> |> 12 3 33 NA 5.2 no
> |> 13 4 11 4.6 NA no
> |>
> |> >Whatever it is, can't you subset them out?
> |> Yes, thatīs the problem. To describe what I mean, I added the
> |> variable needed by hand. I donīt know how to compute such a
> |> variable to subset.
> |>
> |>
> |> My final data frame should look like this:
> |> age school out1 out2 nedded
> |> 1 1 10 9.5 1.1 yes
> |> 6 1 10 2.3 2.0 yes
> |> 9 2 20 3.3 6.5 yes
>
>
> Now that I know what you want, I can see what can work. Undoubtedly,
> there are far more elegant ways of doing it, but this will work.
>
> xx <- merge(treat, control)
> out <- xx[1, ] # set up the out dataframe
> for(i in 2:nrow(xx)){
> x.i <- xx[i, ]
> kk <- logical(4)
> for(j in 1:4)
> kk[j] <- x.i[, j]%in%out[, j]
> if(!kk[4]){ # don't add to output if last column value exists
> if(!all(kk[1:3])) # a different combination of first 3 cols
> out <- rbind(out, x.i)
> }
> }
> out
>
>
> age school out1 out2
> 1 1 10 9.5 1.1
> 6 1 10 2.3 2.0
> 9 2 20 3.3 6.5
>
> If you have very large dataframes, you might like to improve on it.
>
>
> HTH
>
>
> --
> ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
> ___ Patrick Connolly
> {~._.~} Great minds discuss ideas
> _( Y )_ Middle minds discuss events
> (:_~*~_:) Small minds discuss people
> (_)-(_) ..... Anon
>
> ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
>



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 Mon 21 Apr 2008 - 05:47:25 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 Mon 21 Apr 2008 - 06:30:30 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