(My first data sample example, I hope this is enough)
I have this xts-object weights
> dput(head(weights,4))
structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.333333333333333, -0.333333333333333,
-0.333333333333333, -0.333333333333333, 0, 0, 0, 0, 0, 0.333333333333333,
0.333333333333333, 0.333333333333333, 0.333333333333333, 0.333333333333333,
0.333333333333333, 0.333333333333333, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0.333333333333333, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, -0.333333333333333, -0.333333333333333, -0.333333333333333,
-0.333333333333333, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0.333333333333333, 0.333333333333333, 0.333333333333333, 0.333333333333333,
0, 0, 0, 0, -0.333333333333333, -0.333333333333333, -0.333333333333333,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.333333333333333), .indexCLASS = "Date", .indexTZ = "UTC", tclass = "Date", tzone = "UTC", class = c("xts",
"zoo"), index = structure(c(946512000, 949276800, 951782400,
954460800), tzone = "UTC", tclass = "Date"), .Dim = c(4L, 39L
), .Dimnames = list(NULL, c("ABB.N", "ACTELION.N", "ADECCO.N",
"ALUSUISSE.N", "BALOISE.N", "CIBA.SC.N", "CLARIANT.N", "CS.GROUP.N",
"ELEKTROWATT.I", "EMS.CHEMIE", "GEBERIT.N", "GIVAUDAN.N", "JULIUS.BAER.N",
"KUDELSKI.I", "LAFARGEHOLCIM.N", "LONZA.N", "NESTLE.N", "NOBEL.BIOCARE.N",
"NOVARTIS.I", "NOVARTIS.N", "RICHEMONT.N", "ROCHE.GS", "SAIRGROUP.N",
"SBV.N", "SERONO..B..I", "SGS.N", "SULZER.N", "SWATCH.I", "SWATCH.N",
"SWISS.LIFE.HOLDING.AG.N", "SWISS.RE.N", "SWISSCOM.N", "SYNGENTA.N",
"SYNTHES.N", "TRANSOCEAN.N", "UBS.GROUP.N", "UBS.I", "UNAXIS.N20",
"ZURICH.INSURANCE.N")))
and two numeric vectors Pos
and Neg
> dput(head(Pos,4))
c(`1999-12-30` = 0.34191022613305, `2000-01-31` = 0.411653443805752,
`2000-02-29` = 0.393078629707615, `2000-03-31` = 0.376686025847744
)
> dput(head(Neg,4))
c(`1999-12-30` = -1.32059915686837, `2000-01-31` = -1.27821843244263,
`2000-02-29` = -1.3142472840903, `2000-03-31` = -1.32647296498383)
I now want to divide each negative/positive value in weights
by the corresponding row in Neg
or Pos
(if Neg
and Pos
were to be transformed to a matrix/dataframe)
I tried for-loops, apply-functions and simple division with conditions but I never got the desired result which cant be so difficult for such a rather simple task...
From the last problem I had there seems to be also problems with subsetting from xts-objects...
These codes didnt work for me: (mostly errors with dimensions or subscripts...)
For loop:
weights_adj <- apply(weights,1,function(x) if (coredata(x)[coredata(x)>0]){
coredata(x)[coredata(x)]/Pos
} else if (coredata(x)[coredata(x)<0]){
coredata(x)[coredata(x)]/Neg
})
apply function:
weights_adj <- apply(weights,1,function(x) (coredata(x)[coredata(x)>0])/Pos)
which + apply:
which_negative_weight <- t(apply(weights,1,function (x) which(x<0)))
result_negative <- apply(weights,1,function(x) x[,which_negative_weight[1,]])
which + for:
for (i in 1:nrow(weights)){
(weights[i,which_negative_weight[i,]]/Neg[i,])
}
The solution from @K.Hua worked and he helped me very quickly, but @G.Grothendieck's solution is simpler and faster!
You try to divide a sample of a vector of size N by a whole vector of size N. Besides, I think it's easier to do a loop over the columns instead :
weights_adj = weights
neg <- apply(weights,2,function (x) x<0)
for (i in 1:ncol(weights)){
weights_adj[neg[,i],i] = weights[neg[,i],i]/Neg[neg[,i]]
weights_adj[!neg[,i],i] = weights[!neg[,i],i]]/Pos[!neg[,i]]
}