[R] Merge usage to update data.frame

From: Mario Valle <mvalle_at_cscs.ch>
Date: Sun, 06 Jun 2010 18:41:28 +0200

Good morning!
I have two datasets with the same structure, one containing all my values and the second one that update same of these values (see example below).
I want to create a data.frame with all the values of the first dataset except when an entry is present in the second dataset, in which case the second dataset value is taken.

Is there a better method than my last line kludge? Thanks for your help!


t1 <- textConnection("AAA 1
CCC 3")
a <- read.table(t1, stringsAsFactors=FALSE) close(t1)
t2 <- textConnection("BBB 22")
u <- read.table(t2, stringsAsFactors=FALSE) close(t2)
m <- merge(a, u, by="V1", all.x=TRUE)
out <- data.frame(V1=m$V1, V2=ifelse(is.na(m$V2.y), m$V2.x, m$V2.y))

Ing. Mario Valle
Data Analysis and Visualization Group            | 
Swiss National Supercomputing Centre (CSCS)      | Tel:  +41 (91) 610.82.60
v. Cantonale Galleria 2, 6928 Manno, Switzerland | Fax:  +41 (91) 610.82.82

R-help_at_r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Sun 06 Jun 2010 - 16:44:31 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 06 Jun 2010 - 23:40: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.

list of date sections of archive