Hello everyone, I have two data.frames that look like calib: place zoom scale left 0.65 8 left 0.80 5.6 left 1.20 3 right 0.65 8.4 right 0.80 6 right 1.20 2.9 X: ... place zoom .... ... left 0.80 .... ... left 1.20 .... ... right 0.65 .... ... NA NA .... ... right 0.8 .... ... left 1.20 .... and I want to get the corresponding values of 'scale' in a new column in X, i.e.: X: ... place zoom .... scale ... left 0.80 .... 5.6 ... left 1.20 .... 3 ... right 0.65 .... 8.4 ... NA NA .... NA ... right 0.8 .... 6 ... left 1.20 .... 3 I tried various combination of `which` and `match` but could not make it work. I fell back on defining a custom function and applying it over the lines get.scales <- function(x) { if (is.na(x$zoom)) { scaleF = NA } else { scaleF = calib[calib$zoom==x$zoom & calib$place==x$place, "scale"] } return(scaleF) } and apply(X, 1, get.scales) but: - this is not very practical since using apply apparently converts the data to an array and looses column titles (i.e. x$size does not work, I need to use column numbers, which I'd rather not) - I feel there is an easier, vector based way. I would welcome suggestions. Thank you in advance. JiHO --- http://jo.irisson.free.fr/
Charles C. Berry
2008-Jan-18 19:13 UTC
[R] Selecting rows conditionally between 2 data.frames
Looks like a job for ?merge HTH, Chuck On Fri, 18 Jan 2008, jiho wrote:> Hello everyone, > > I have two data.frames that look like > > calib: > place zoom scale > left 0.65 8 > left 0.80 5.6 > left 1.20 3 > right 0.65 8.4 > right 0.80 6 > right 1.20 2.9 > > X: > ... place zoom .... > ... left 0.80 .... > ... left 1.20 .... > ... right 0.65 .... > ... NA NA .... > ... right 0.8 .... > ... left 1.20 .... > > and I want to get the corresponding values of 'scale' in a new column > in X, i.e.: > X: > ... place zoom .... scale > ... left 0.80 .... 5.6 > ... left 1.20 .... 3 > ... right 0.65 .... 8.4 > ... NA NA .... NA > ... right 0.8 .... 6 > ... left 1.20 .... 3 > > I tried various combination of `which` and `match` but could not make > it work. > I fell back on defining a custom function and applying it over the lines > get.scales <- function(x) > { > if (is.na(x$zoom)) { > scaleF = NA > } else { > scaleF = calib[calib$zoom==x$zoom & calib$place==x$place, "scale"] > } > return(scaleF) > } > and > apply(X, 1, get.scales) > but: > - this is not very practical since using apply apparently converts the > data to an array and looses column titles (i.e. x$size does not work, > I need to use column numbers, which I'd rather not) > - I feel there is an easier, vector based way. > > I would welcome suggestions. Thank you in advance. > > JiHO > --- > http://jo.irisson.free.fr/ > > ______________________________________________ > 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. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901