Taking my first question as starting point: Split data frame into multiple data frames based on information in a xts object
Now I have a new problem.
Suppose you have double entries in your data.frames d1 & d2 - i.e. you have in d1 the letter "h" in grp B & A and therefore also two time series for "a" in d2. How can we solve this?
d1 <- data.frame(grp=sample(LETTERS[1:4], 11, replace=TRUE),
name=letters[c(8,1:10)])
> d1
grp name
1 B h
2 D a
3 B b
4 D c
5 B d
6 C e
7 A f
8 A g
9 A h
10 B i
11 C j
d2 <- matrix(round(runif(55), 2), ncol=11)
colnames(d2) <- letters[c(8,1:10)]
library(xts)
d2 <- xts(d2, seq.Date(as.Date('01-01-2011', '%d-%m-%Y'),
as.Date('5-01-2011', '%d-%m-%Y'), 1))
> d2
h a b c d e f g h i j
2011-01-01 0.04 0.77 0.49 0.87 0.23 0.95 0.69 0.35 0.14 0.47 0.25
2011-01-02 0.73 0.46 0.28 0.86 0.75 0.08 0.00 0.89 0.50 0.12 0.54
2011-01-03 0.36 0.61 0.92 0.80 0.12 0.25 0.18 0.44 0.73 0.19 0.30
2011-01-04 0.18 0.65 0.68 0.44 0.54 0.84 0.13 0.64 0.54 0.81 0.73
2011-01-05 0.58 0.55 0.10 0.33 0.55 0.23 0.82 0.21 0.58 0.24 0.04
This does not work:
out <- setNames(sapply(unique(d1$grp), function(x) {
d2[, which(d1$grp[match(colnames(d2), d1$name)] == x)]
}), unique(d1$grp))
out
$B
h b d h i
2011-01-01 0.04 0.49 0.23 0.14 0.47
2011-01-02 0.73 0.28 0.75 0.50 0.12
2011-01-03 0.36 0.92 0.12 0.73 0.19
2011-01-04 0.18 0.68 0.54 0.54 0.81
2011-01-05 0.58 0.10 0.55 0.58 0.24
$D
a c
2011-01-01 0.77 0.87
2011-01-02 0.46 0.86
2011-01-03 0.61 0.80
2011-01-04 0.65 0.44
2011-01-05 0.55 0.33
$C
e j
2011-01-01 0.95 0.25
2011-01-02 0.08 0.54
2011-01-03 0.25 0.30
2011-01-04 0.84 0.73
2011-01-05 0.23 0.04
$A
f g
2011-01-01 0.69 0.35
2011-01-02 0.00 0.89
2011-01-03 0.18 0.44
2011-01-04 0.13 0.64
2011-01-05 0.82 0.21
Expected output should be:
out
$B
h b d i
2011-01-01 0.04 0.49 0.23 0.47
2011-01-02 0.73 0.28 0.75 0.12
2011-01-03 0.36 0.92 0.12 0.19
2011-01-04 0.18 0.68 0.54 0.81
2011-01-05 0.58 0.10 0.55 0.24
$D
a c
2011-01-01 0.77 0.87
2011-01-02 0.46 0.86
2011-01-03 0.61 0.80
2011-01-04 0.65 0.44
2011-01-05 0.55 0.33
$C
e j
2011-01-01 0.95 0.25
2011-01-02 0.08 0.54
2011-01-03 0.25 0.30
2011-01-04 0.84 0.73
2011-01-05 0.23 0.04
$A
f g h
2011-01-01 0.69 0.35 0.14
2011-01-02 0.00 0.89 0.50
2011-01-03 0.18 0.44 0.73
2011-01-04 0.13 0.64 0.54
2011-01-05 0.82 0.21 0.58
Help is much appreciated!
Thank you in advance...