如何在数据框列上进行标量乘法 [英] How to do scalar multiply on data frame column

查看:58
本文介绍了如何在数据框列上进行标量乘法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确定这个人正坐在我的面前,但我看不到它.我有一个数据框"a",例如:

I’m sure this one is sitting right in front of me but I can’t see it. I have a data frame, "a", such that:

>a
         Chars    Numbers
This      A        15
That      B        22
Other     C        18

我只是想将Numbers列乘以标量,例如b< -10,并保持数据帧的其他部分不变.(我将在结果的Numbers列中得到150、220、180,但行/列标题和Chars列却相同.)

I simply want to multiply the Numbers column by a scalar, say b <- 10, and keep the other parts of the data frame intact. (I would get 150, 220, 180 in the Numbers column of the result, but the same row/column headings and Chars column.)

不起作用的是* b,尝试使用apply或a $ Numbers * b.

What does not work is a * b, trying to use apply, or a$Numbers * b.

推荐答案

尝试以下操作:

a$Numbers <- a$Numbers * b

为了解释正在发生的事情,让我们使用示例中的数字:

To explain what's happening, let's use the figures from your example:

上面的代码将允许您在 Numbers 列上使用标量乘法:

The above code will allow you to use scalar multiplication on the Numbers column:

然后您将分配回 a 中的 Numbers 列:

Which you'll then assign back to the Numbers column in a:

这篇关于如何在数据框列上进行标量乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆