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

Dimitrios Rizopoulos Dimitris.Rizopoulos at med.kuleuven.be
Thu May 18 17:15:36 CEST 2006


maybe merge() is what you're looking for, e.g.,

dat <- data.frame(x = rnorm(10), v1 = gl(5, 2), v2 = rep(1:2, 5))
ids <- data.frame(v1 = c("2", "3", "4"), v2 = c(1, 2, 1))
#############################
dat
merge(dat, ids)

I hope it helps.

Best,
Dimitris

-- 
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


Quoting "Antonio, Fabio Di Narzo" <antonio.fabio at gmail.com>:

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


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm




More information about the R-help mailing list