[R] data frames; matching/merging

jim holtman jholtman at gmail.com
Mon Feb 8 17:49:29 CET 2010


> x <- read.table(textConnection("    V1     V2
+ 1    a        3
+ 2    a        2
+ 3    b        9
+ 4    c        4
+ 5    a        7
+ 6    b        11"), header=TRUE)
> closeAllConnections()
> # close; matrix with rownames - easy enough to change into a dataframe if you want
> cbind(tapply(x$V2, x$V1, min))
  [,1]
a    2
b    9
c    4
>


On Mon, Feb 8, 2010 at 11:39 AM, Jonathan <jonsleepy at gmail.com> wrote:
> Hi all,
>    I'm feeling a little guilty to ask this question, since I've
> written a solution using a rather clunky for loop that gets the job
> done.  But I'm convinced there must be a faster (and probably more
> elegant) way to accomplish what I'm looking to do (perhaps using the
> "merge" function?).  I figured somebody out there might've already
> figured this out:
>
> I have a dataframe with two columns (let's call them V1 and V2).  All
> rows are unique, although column V1 has several redundant entries.
>
> Ex:
>
>     V1     V2
> 1    a        3
> 2    a        2
> 3    b        9
> 4    c        4
> 5    a        7
> 6    b        11
>
>
> What I'd like is to return a dataframe cut down to have only unique
> entires in V1.  V2 should contain a vector, for each V1, that is the
> minimum of all the possible choices from the set of redundant V1's.
>
> Example output:
>
>      V1     V2
> 1     a        2
> 2     b        9
> 3     c        4
>
>
> If somebody could (relatively easily) figure out how to get closer to
> a solution, I'd appreciate hearing how.  Also, I'd be interested to
> hear how you came upon the answer (so I can get better at searching
> the R resources myself).
>
> Regards,
> Jonathan
>
> ______________________________________________
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list