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, <[EMAIL PROTECTED]> 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@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
[EMAIL PROTECTED]
______________________________________________
R-help@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.