如何将数据帧的一列除以另一列? [英] How can I divide one column of a data frame through another?

查看:129
本文介绍了如何将数据帧的一列除以另一列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一栏除以每人的时间,我该怎么做?我找不到关于如何分割的任何内容。

I wanted to divide one column by another to get the per person time how can I do this?I couldn't find anything on how you can divide.

这是我要使用的一些数据

Here is some data that I want to use

     min    count2.freq
263807.0    1582
196190.5    1016
586689.0    3479

最后,我想添加带有数字的第三列从 min / count2.freq

In the end I want to add a third column like this that has the number from min / count2.freq

例如 263808.0 / 1582 = 166.75

推荐答案

有很多方法可以完成此操作。问题是如何使R知道要划分的变量的位置。

There are a plethora of ways in which this can be done. The problem is how to make R aware of the locations of the variables you wish to divide.

假设

d <- read.table(text = "263807.0    1582
196190.5    1016
586689.0    3479
")
names(d) <- c("min", "count2.freq")
> d
       min count2.freq
1 263807.0        1582
2 196190.5        1016
3 586689.0        3479



我的首选方式



要将所需的除法添加为第三个变量,可以使用 transform()

> d <- transform(d, new = min / count2.freq)
> d
       min count2.freq      new
1 263807.0        1582 166.7554
2 196190.5        1016 193.1009
3 586689.0        3479 168.6373



基本的R方式



如果在函数中执行此操作(即您正在编程),则最好避免加糖如上所示和索引。在那种情况下,这些都可以满足您的要求

The basic R way

If doing this in a function (i.e. you are programming) then best to avoid the sugar shown above and index. In that case any of these would do what you want

## 1. via `[` and character indexes
d[, "new"] <- d[, "min"] / d[, "count2.freq"]

## 2. via `[` with numeric indices
d[, 3] <- d[, 1] / d[, 2]

## 3. via `$`
d$new <- d$min / d$count2.freq

所有这些都可以在提示符下使用,但是更易于阅读:

All of these can be used at the prompt too, but which is easier to read:

d <- transform(d, new = min / count2.freq)

d$new <- d$min / d$count2.freq ## or any of the above examples

希望您像我一样并且第一个版本更好;-)

Hopefully you think like I do and the first version is better ;-)

我们不使用 tranform()等编程时,是因为它们如何进行求值(查找命名变量)。在最高级别(提示时,以交互方式工作) transform()等也可以正常工作。但是埋在函数调用中或在对 apply()函数家族之一的调用中,它们可以并且经常中断。

The reason we don't use the syntactic sugar of tranform() et al when programming is because of how they do their evaluation (look for the named variables). At the top level (at the prompt, working interactively) transform() et al work just fine. But buried in function calls or within a call to one of the apply() family of functions they can and often do break.

同样,请小心使用数字索引(上面的 ## 2。);如果更改数据顺序,则会选择错误的变量。

Likewise, be careful using numeric indices (## 2. above); if you change the ordering of your data, you will select the wrong variables.

如果您只是想进行除法(而不是将结果重新插入数据框中,请使用 with(),这使我们能够隔离要评估的简单表达式

If you are just wanting to do the division (rather than insert the result back into the data frame, then use with(), which allows us to isolate the simple expression you wish to evaluate

> with(d, min / count2.freq)
[1] 166.7554 193.1009 168.6373

这再次比等效代码更简洁

This is again much cleaner code than the equivalent

> d$min / d$count2.freq
[1] 166.7554 193.1009 168.6373

,因为它明确指出使用 d ,执行代码 min / count2 .freq 。您的偏好可能与我的不同,所以我显示了所有选项。

as it explicitly states that "using d, execute the code min / count2.freq. Your preference may be different to mine, so I have shown all options.

这篇关于如何将数据帧的一列除以另一列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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