R - 选择列的第 k 个元素

发布于 2024-09-28 21:28:55 字数 770 浏览 1 评论 0原文

我已经在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾听心声的旋律 2024-10-05 21:28:55

您可以按名称和/或位置进行索引。尝试

D[1,"D1"] <- TwinWeight[1,3] * D[ TwinWeight[1,2],"D1"]

将右侧所选的两个标量的乘积分配给 D1 列的第 1 行。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文