[R] Matched pairs with two data frames

ukoenig at med.uni-marburg.de ukoenig at med.uni-marburg.de
Mon Apr 21 07:43:58 CEST 2008


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



More information about the R-help mailing list