将 data.frame 的每个单元格与其权重相乘
我想做的事情很简单——但我失败了。
我有一个包含“字符”和“数字”的 data.frame。 data.frame 的其中一列代表权重。
我想将数据框的每个单元格乘以相应的权重(如果它是数字)。
我该怎么做(最好不使用嵌套循环)。
先感谢您!
示例:
c1 c2 w
l1 abc 2 1
l2 dxf 3 0.5
l3 ghi 4 1.5
应该成为
c1 c2 w
l1 abc 2 1
l2 dxf 1.5 0.5
l3 ghi 6 1.5
What I want to do is embarrassing simple - nevertheless I fail.
I have a data.frame with "characters" and "numerics". One of the columns of the data.frame represents the weights.
I want to multiply every cell of the data frame with the corresponding weight (if it's a numeric).
How do I do that (best without using a nested loop).
Thank you in advance!
Example:
c1 c2 w
l1 abc 2 1
l2 dxf 3 0.5
l3 ghi 4 1.5
should become
c1 c2 w
l1 abc 2 1
l2 dxf 1.5 0.5
l3 ghi 6 1.5
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于可重现的示例,
dd
是一个混合变量类型的数据帧,W
是权重。For a reproducible example,
dd
is a data frame with a mixture of variable types, withW
being the weights.矢量化!
实际上,您需要
c2 * w
,但我们需要告诉 R 查看数据框的内部:我们可以将其插回到
dat
中一行:(如果您想覆盖现有的
c2
,请将c3
替换为c2
。)如果您有多个数字列,除了权重,如果您想自动化它,则需要稍微不同的策略(即不告诉 R 哪些列要乘以
w
)。Vectorise!
Effectively, you want
c2 * w
, but we need to tell R to look inside the data frame:Which we can insert back into
dat
in a single line:(Replace
c3
withc2
if you want to overwrite the existingc2
.)If you have more than one numeric column other than weights, a slighlty different strategy is required if you want to automate it (i.e. not tell R which columns to multiply by
w
).只是为了尝试将其写成一行(但实际上不是最具可读性的!):
Just for the pleasure to try to make it in one line (but really not the most readable !) :
正如您所看到的,有多种方法可以做到这一点,但不知何故,您会期望一种非常简单的方法,而我不知道这种方法是否存在。 plyr 包中有一个名为 colwise 的库函数,它很接近,但我无法想出一个干净的方法来让它完全按照你想要的方式去做。我可以用 colwise 做的最好的事情是(假设您的数据框名为 df):
<代码>
对于那些熟悉 colwise 的人,我认为您不能简单地使用 numcolwise,因为这样根本不会发出非数字列。我想不出任何干净的方法来不将乘法应用于重量,这就是为什么我只是在这里保存和恢复它。我认为如果可以找到一种更简洁的方法来做到这一点,那么 colwise 是一种很好的简单且易于理解的方法。
As you have seen, there a number of ways to do this, but somehow you'd expect one really simple way and I don't know if that exists. There is a library function in the plyr package called colwise that is close, but I can't come up with a clean way to get it to do exactly what you want. The best I can do wtih colwise is this (assuming your dataframe is named df):
For those who are familiar with colwise, I don't think you can simply use numcolwise because then the non-numeric columns are not emitted at all. And I can't figure out any clean way to not have the multiplication appled to the weight, which is why I simply save and restore it here. I think if a cleaner way of doing this can be worked out, colwise is a nice simlpe and easy to understand way to do this.