I have performed statistical matching in R. For each case "VAR2002", I have found one or more statistical twin(s) "VAR2004". In R, I have a data frame "TwinWeight" like this:
VAR2002 VAR2004 Weight
1 2955 1.00000000
2 3961 1.00000000
3 2913 0.33333333
3 3430 0.33333333
3 3554 0.33333333
4 2996 0.20000000
4 4618 0.20000000
. . .
Additionally, I have a dataset D with many variables D1, D2,..., D55.
With this knowledge, I want to make my calculations for all D variables in the following way: For each VAR2002: Weight*D[Statistical Twin 1]
This means for VAR2002=1 and D1:
D$D1[1]<-TwinWeight[1,3]*D$D1[TwinWeight[1,2]]
Unfortunately, this command is not valid in R, as D$D1[] needs a numeric value in []. I cannot name a numeric value, but rather want this number to be taken from the TwinWeight table.
Does anybody know how to solve this problem?
开发者_如何转开发Thanks so much for your help!
You can index by name and / or position. Try
D[1,"D1"] <- TwinWeight[1,3] * D[ TwinWeight[1,2],"D1"]
which assigns to row 1 of column D1 the product of the two scalars selected on the right.
精彩评论