[R] extracting values from data.frame given arbitrary keys

Jason Barnhart jasoncbarnhart at msn.com
Thu May 18 18:19:31 CEST 2006


?subset is handy.

You could build a 2nd data.frame containing the extraction value 
combinations and use merge to get the inner join results.  If a combination 
is not known to exist the result will be extracted , otherwise no harm is 
done.


----- Original Message ----- 
From: "Antonio, Fabio Di Narzo" <antonio.fabio at gmail.com>
To: <R-help at stat.math.ethz.ch>
Sent: Thursday, May 18, 2006 7:04 AM
Subject: [R] extracting values from data.frame given arbitrary keys


> Hi all.
> I have a pure R programming question.
> Having a data.frame with a 'data' variable and some (>1) ID variables
> (either numeric or factors), I would extract a sequence of data values 
> given
> a sequence of ID values.
>
> Here's an example. Build a dataset:
> n1 <- 5
> n2 <- 2
> v1 <- rep(factor(1:n1),2)
> v2 <- rep(1:n2,5)
> x <- rnorm(10)
> dat <- data.frame(x=x, v1=v1, v2=v2)
>
> Now, for example, I should extract x values responding to some arbitrary
> IDs:
> (v1="2", v2=1), (v1="3", v2=2), (v1="4", v2=1).
> For example, I should store those id sequences in a separate list (maybe 
> an
> indexing data.frame), but this really isn't the point.
> The point is that I have somewhere a ready sequence of IDs from which
> obtaining corresponding data.
> Note that I don't know if some ID combination exists in the database.
> Some suggestions on a good way to do this?
>
> Tnx all,
> Antonio, Fabio Di Narzo.
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list