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
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 > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
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, <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
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
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, <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 > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
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=runif(6))
> B
day x
1 3 0.5096328
2 4 0.6043132
3 5 0.7947639
4 6 0.7619096
5 7 0.1571041
6 8 0.3473159> D <- merge(A,B, by='day',all=TRUE)
> D$diff <- D$x.x - D$x.y
> D
day x.x x.y diff
1 1 0.9764534 NA NA
2 2 0.9693998 NA NA
3 3 0.1324933 0.5096328 -0.3771395
4 4 0.8311153 0.6043132 0.2268022
5 5 0.3264465 0.7947639 -0.4683174
6 6 NA 0.7619096 NA
7 7 NA 0.1571041 NA
8 8 NA 0.3473159 NA
-Don
At 2:26 PM -0700 7/26/08, <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
>PLEASE do read the posting guide http:// www.
R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.
--
---------------------------------
Don MacQueen
Lawrence Livermore National Laboratory
Livermore, CA, USA
925-423-1062
macq at llnl.gov