[R] functionality of "update" in SAS

Deepayan Sarkar deepayan.sarkar at gmail.com
Wed Sep 20 22:08:43 CEST 2006


On 9/20/06, Denis Chabot <chabotd at globetrotter.net> wrote:
> Dear list,
>
> I've tried to search the archives but found nothing, although I may
> use the wrong wording in my searches. I've also double-checked the
> upData function in Hmisc, but it does something else.
>
> I'm wondering if one can update a dataframe by "forcing into" it a
> shorter dataframe containing the corrections, like the "update"
> provided in SAS data steps.
>
> In this simple example:
> a <- data.frame(id=c(1:5),x=rnorm(5))
> b <- data.frame(id=4,x=rnorm(1))
>  > a
>    id          x
> 1  1  0.6557921
> 2  2  0.1897523
> 3  3  0.7976721
> 4  4  0.2107103
> 5  5 -0.8855786
>  > b
>    id         x
> 1  4 0.8369147
>
> I would like the "updated" dataframe to look like (row names are not
> important to me)
>
>     id          x
> 1   1  0.6557921
> 2   2  0.1897523
> 3   3  0.7976721
> 4   4  0.8369147
> 5   5 -0.8855786

Making a few assumtions (like id's being unique, b$id guaranteed to be
in a$id and all columns in b are also in a), you could do

which.id <- which(a$id %in% b$id)
a[which.id, colnames(b)] <- b

-Deepayan



More information about the R-help mailing list