如何将数据框的一列与另一列分开? [英] How can I divide one column of a data frame through another?

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

问题描述

我想将一列除以另一列以获得人均时间,我该怎么做?我找不到有关如何划分的任何信息.

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天全站免登陆