简体   繁体   English

如何计算两个数据帧的元素商?

[英]How to calculate an element-wise quotient of two data frames?

> A <- data.frame(x = c(1,2,3), y = c(4,5,6), z = c(7,8,9))
> B <- data.frame(x = c(1,1,1), y = c(2,2,2), z = c(3,3,3))
> A
  x y z
1 1 4 7
2 2 5 8
3 3 6 9
> B
  x y z
1 1 2 3
2 1 2 3
3 1 2 3

What I would like to do is calculate a new data frame C which is the defined as: 我想要做的是计算一个新的数据框C,定义为:

C[i,j] := A[i,j] / B[i,j]

for all coordinates i,j possible. 对于所有坐标i,j可能。

Is there a clean and quick way to do it without resorting to loops and without referencing individual columns or rows ? 是否有一个干净,快速的方法来做到这一点, 而不诉诸循环没有引用单个列或行

(Application of data.table, plyr is fine) (data.table的应用,plyr很好)

Simple: do A/B : 简单:做A/B

R> C <- A/B
R> C
  x   y       z
1 1 2.0 2.33333
2 2 2.5 2.66667
3 3 3.0 3.00000
R> 

R really is a vectorised language. R 真的是一种矢量化语言。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM