Re: [R] Data length mismatch.

2008-07-26 Thread Don MacQueen
I think the merge() function should be adequate for this task. Here is an example. A <- data.frame(day=1:5, x=runif(5)) B <- data.frame(day=3:7, x=runif(5)) A day x 1 1 0.9764534 2 2 0.9693998 3 3 0.1324933 4 4 0.8311153 5 5 0.3264465 B <- data.frame(day=3:8, x=run

Re: [R] Data length mismatch.

2008-07-26 Thread Gabor Grothendieck
Sorry, the last one should be: ix <- match(B$DayOfYear, A$DayOfYear) A[ix, "x"] <- A[ix, "x"] - B$x Again we are assuming B's days are a subset of A's. On Sat, Jul 26, 2008 at 6:08 PM, Gabor Grothendieck <[EMAIL PROTECTED]> wrote: > Here is a third solution. > > A[B$DayOfYear, "x"] <- A[B$DayOfY

Re: [R] Data length mismatch.

2008-07-26 Thread Gabor Grothendieck
Here is a third solution. A[B$DayOfYear, "x"] <- A[B$DayOfYear, "x"] - B$x Its assumes B's days are a subset of A's but if that's not the case then you would need to intersect them first: ?intersect On Sat, Jul 26, 2008 at 5:26 PM, <[EMAIL PROTECTED]> wrote: > I have two vectos (list) that repr

Re: [R] Data length mismatch.

2008-07-26 Thread Gabor Grothendieck
Here is a second solution. This one uses sqldf instead of zoo: library(zoo) sqldf("select A.x - ifnull(B.x, 0) from A left join B using(DayOfYear)") See http://sqldf.googlecode.com On Sat, Jul 26, 2008 at 5:26 PM, <[EMAIL PROTECTED]> wrote: > I have two vectos (list) that represent a years of

Re: [R] Data length mismatch.

2008-07-26 Thread Gabor Grothendieck
For the last statement we may prefer this so it stays a zoo object: > m <- merge(Az, Bz, fill = 0) > m[,1] - m[,2] 12345 14290 30490 2219 On Sat, Jul 26, 2008 at 5:53 PM, Gabor Grothendieck <[EMAIL PROTECTED]> wrote: > Look at merge.zoo > >> library(zoo) >> dput(A) > st

Re: [R] Data length mismatch.

2008-07-26 Thread Gabor Grothendieck
Look at merge.zoo > library(zoo) > dput(A) structure(list(DayOfYear = 1:5, x = c(1429L, 3952L, 3049L, 2844L, 2219L)), .Names = c("DayOfYear", "x"), class = "data.frame", row.names = c("1", "2", "3", "4", "5")) > B <- A[c(2,4),] > Az <- zoo(A$x, A$DayOfYear) > Bz <- zoo(B$x, B$DayOfYear) > merge(Az

[R] Data length mismatch.

2008-07-26 Thread rkevinburton
I have two vectos (list) that represent a years of data. Each "row" is represented by the day of year and the quantity that was sold for that day. I would like to form a new vector that is the difference between the two years of data. A sample of A (and similarly B) looks like: > A[1:5,] Day