R - 选择列的第 k 个元素
我已经在 R 中执行了统计匹配。对于每种情况“VAR2002”,我都找到了一个或多个统计双胞胎“VAR2004”。 在 R 中,我有一个像这样的数据框“TwinWeight”:
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
。 。 。
此外,我有一个数据集 D,其中包含许多变量 D1、D2、...、D55。
有了这些知识,我想通过以下方式对所有 D 变量进行计算: 对于每个 VAR2002: Weight*D[Statistical Twin 1]
这对于 VAR2002=1 和 D1 意味着:
D$D1[1]<-TwinWeight[1,3]*D$D1[TwinWeight[1,2]]
不幸的是,该命令在 R 中无效,因为 D$D1[] 需要 [] 中的数值。我无法命名一个数值,而是希望从 TwinWeight 表中获取该数字。
有人知道如何解决这个问题吗?
非常感谢您的帮助!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以按名称和/或位置进行索引。尝试
将右侧所选的两个标量的乘积分配给 D1 列的第 1 行。
You can index by name and / or position. Try
which assigns to row 1 of column D1 the product of the two scalars selected on the right.