[R] merge, rbind, merge.zoo? adding new rows to existing data.frame

Ad Flan aj.flan at gmail.com
Wed Feb 9 07:10:05 CET 2011


Hi all,
I'm trying to add updated data to an existing time series where an
overlap exists. I need to give priority to the update data.
My script runs every morning to collect data the updated data. There
are quite often varied lengths, so once off solutions identifying rows
to solve this example won't work.

I've experimented with merge, rbind, merge.zoo, but to no avail.

An example

existing <- data.frame(
date = c("17-01-2011", "18-01-2011", "19-01-2011", "20-01-2011", "21-01-2011"),
    data = c(5, 5, 5, 5, 23))
existing$date <- as.Date(existing$date, "%d-%m-%Y")

update <- data.frame(
date = c("20-01-2011", "21-01-2011", "22-01-2011"),
    data = c(6, 22, 6))
update$date <- as.Date(update$date, "%d-%m-%Y")

merge(existing, update, all.x = TRUE)
#This will only keep existing values

#structure is
>str(existing)
'data.frame':   5 obs. of  2 variables:
 $ date:Class 'Date'  num [1:5] 14991 14992 14993 14994 14995
 $ data: num  5 5 5 5 23

> str(update)
'data.frame':   3 obs. of  2 variables:
 $ date:Class 'Date'  num [1:3] 14994 14995 14996
 $ data: num  6 22 6

# The output should be:
#               date data
# 2011-01-17    5        #(from existing)
# 2011-01-18    5        #(from existing)
# 2011-01-19    5        #(from existing)
# 2011-01-20    6        #(from update)
# 2011-01-21   22       #(from update)
# 2011-01-22    6        #(from update)

Any ideas?

Many thanks, Adam Flanagan



More information about the R-help mailing list