Antonio, Fabio Di Narzo
2006-May-18 14:04 UTC
[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]]
Dimitrios Rizopoulos
2006-May-18 15:15 UTC
[R] extracting values from data.frame given arbitrary keys
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
Jason Barnhart
2006-May-18 16:19 UTC
[R] extracting values from data.frame given arbitrary keys
?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 >