# Re: [R] Data length mismatch.

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Sat, 26 Jul 2008 17:59:47 -0400

For the last statement we may prefer this so it stays a zoo object:

> m <- merge(Az, Bz, fill = 0)
> m[,1] - m[,2]

1 2 3 4 5
1429 0 3049 0 2219

On Sat, Jul 26, 2008 at 5:53 PM, Gabor Grothendieck <ggrothendieck_at_gmail.com> wrote:
> 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, Bz, fill = 0)
> Az Bz
> 1 1429 0
> 2 3952 3952
> 3 3049 0
> 4 2844 2844
> 5 2219 0
>> merge(Az, Bz, fill = 0) %*% c(1, -1)
> [,1]
> [1,] 1429
> [2,] 0
> [3,] 3049
> [4,] 0
> [5,] 2219
>
>
> On Sat, Jul 26, 2008 at 5:26 PM, <rkevinburton_at_charter.net> wrote:
>> 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,]
>> DayOfYear x
>> 1 1 1429
>> 2 2 3952
>> 3 3 3049
>> 4 4 2844
>> 5 5 2219
>>>
>>
>> D <- A - B
>>
>> This works just fine if A and B are both the same length. How is the best way to handle the situation where A and B are of different lengths? If the day of year exists in both vectors (lists) then I just want the coorespondng "row" in D to be the difference btween A and B values. If the "row" doesn't exist in either A or B then the difference should be treated as if the missing "row" was zero. Is this feasible?
>>
>> Thank you.
>>
>> Kevin
>>
>> ______________________________________________
>> R-help_at_r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help