I am sure this is not the most elegant method, but it will "work".
> new <- merge(nn,uu, by = c("a","b"), all.x=T)
> new
a b x.x y.x x.y y.y
1 1 1 0.03648491 0.69644753 100 -100
2 1 2 0.76826365 0.28821953 NA NA
3 1 3 0.62744363 0.08373154 NA NA
4 2 1 0.27503185 0.55738251 NA NA
5 2 2 0.45890263 0.80790679 200 -100
6 2 3 0.19193329 0.88803516 NA NA
7 3 1 0.36065586 0.72215405 NA NA
8 3 2 0.93962690 0.10943878 NA NA
9 3 3 0.68443490 0.06951730 300 -100
10 4 1 0.52525832 0.29631353 400 -100
11 4 2 0.56332173 0.63063714 NA NA
12 4 3 0.37129221 0.40779830 NA NA
> new$y <- with( new, (ifelse(!is.na(x.y), y.y, y.x) ) )
> new
a b x.x y.x x.y y.y y
1 1 1 0.03648491 0.69644753 100 -100 -100.00000000
2 1 2 0.76826365 0.28821953 NA NA 0.28821953
3 1 3 0.62744363 0.08373154 NA NA 0.08373154
4 2 1 0.27503185 0.55738251 NA NA 0.55738251
5 2 2 0.45890263 0.80790679 200 -100 -100.00000000
6 2 3 0.19193329 0.88803516 NA NA 0.88803516
7 3 1 0.36065586 0.72215405 NA NA 0.72215405
8 3 2 0.93962690 0.10943878 NA NA 0.10943878
9 3 3 0.68443490 0.06951730 300 -100 -100.00000000
10 4 1 0.52525832 0.29631353 400 -100 -100.00000000
11 4 2 0.56332173 0.63063714 NA NA 0.63063714
12 4 3 0.37129221 0.40779830 NA NA 0.40779830
> new$x <- with( new, (ifelse(!is.na(x.y), x.y, x.x) ) )
> new
a b x.x y.x x.y y.y y x
1 1 1 0.03648491 0.69644753 100 -100 -100.00000000 100.0000000
2 1 2 0.76826365 0.28821953 NA NA 0.28821953 0.7682636
3 1 3 0.62744363 0.08373154 NA NA 0.08373154 0.6274436
4 2 1 0.27503185 0.55738251 NA NA 0.55738251 0.2750319
5 2 2 0.45890263 0.80790679 200 -100 -100.00000000 200.0000000
6 2 3 0.19193329 0.88803516 NA NA 0.88803516 0.1919333
7 3 1 0.36065586 0.72215405 NA NA 0.72215405 0.3606559
8 3 2 0.93962690 0.10943878 NA NA 0.10943878 0.9396269
9 3 3 0.68443490 0.06951730 300 -100 -100.00000000 300.0000000
10 4 1 0.52525832 0.29631353 400 -100 -100.00000000 400.0000000
11 4 2 0.56332173 0.63063714 NA NA 0.63063714 0.5633217
12 4 3 0.37129221 0.40779830 NA NA 0.40779830 0.3712922
> new[ , c("a", "b", "x", "y")]
a b x y
1 1 1 100.0000000 -100.00000000
2 1 2 0.7682636 0.28821953
3 1 3 0.6274436 0.08373154
4 2 1 0.2750319 0.55738251
5 2 2 200.0000000 -100.00000000
6 2 3 0.1919333 0.88803516
7 3 1 0.3606559 0.72215405
8 3 2 0.9396269 0.10943878
9 3 3 300.0000000 -100.00000000
10 4 1 400.0000000 -100.00000000
11 4 2 0.5633217 0.63063714
12 4 3 0.3712922 0.40779830
--
David Winsemius
On Apr 2, 2009, at 7:09 PM, Seeliger.Curt at epamail.epa.gov wrote:
> Folks,
>
> Updating values in a table is simple in SAS or SQL, but I've wracked
> my
> brain looking for an elegant solution in R to no avail thus far.
> Certainly
> this is a common need that's been solved in dozens of different ways.
>
> Given an initial dataframe nn and a smaller dataframe of updates uu,
> I'd
> like to replace the values in
> nn <- expand.grid('a'=1:4, 'b'=1:3)
> nn$x <- runif(nrow(nn))
> nn$y <- runif(nrow(nn))
> uu <- rbind(data.frame('a'=1, 'b'=1, 'x'=100,
'y'=-100)
> ,data.frame('a'=2, 'b'=2, 'x'=200,
'y'=-100)
> ,data.frame('a'=4, 'b'=1, 'x'=400,
'y'=-100)
> ,data.frame('a'=3, 'b'=3, 'x'=300,
'y'=-100)
> )
>
> I'd like the result <- f(nn, uu) to look like
> a b x y
> 1 1 1 100 -100
> 2 2 1 0.8310890 0.2516533
> 3 3 1 0.4304501 0.3004313
> 4 4 1 400 -100
> 5 1 2 0.9546661 0.2935739
> 6 2 2 200 -100
> 7 3 2 0.6941463 0.9356967
> 8 4 2 0.9642832 0.9253482
> 9 1 3 0.8735154 0.6055080
> 10 2 3 0.6665369 0.2938112
> 11 3 3 300 -100
> 12 4 3 0.7268327 0.4536969
>
> Anyone spare a clue about what f() might look like?
> Thanks,
> cur
>
> --
> Curt Seeliger, Data Ranger
> Raytheon Information Services - Contractor to ORD
> seeliger.curt at epa.gov
> 541/754-4638
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
David Winsemius, MD
Heritage Laboratories
West Hartford, CT