Re: [R] functionality of "update" in SAS

From: Deepayan Sarkar <deepayan.sarkar_at_gmail.com>
Date: Wed 20 Sep 2006 - 20:08:43 GMT

On 9/20/06, Denis Chabot <chabotd@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



R-help@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 and provide commented, minimal, self-contained, reproducible code. Received on Thu Sep 21 06:25:29 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 20 Sep 2006 - 20:31:28 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.