在R中划分两个数据帧(一个到另一个) [英] Dividing Two Data Frames (One into the Other) in R

查看:29
本文介绍了在R中划分两个数据帧(一个到另一个)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将一个数据帧除以另一个?这两个数据帧具有相同的列和相同的行,但是我需要将每个相交及其相应的相交划分为一个新的数据帧,例如下方:

How would I divide one data frame by another? The two data frames have the same columns and same rows, but I need to divide every intersect with its corresponding intersect into a new data frame, e.g. below:

DF1
Name    Jan    Feb    Mar
Aaron     2      4      3
Blake     5      6      4

DF2
Name    Jan    Feb    Mar
Aaron     4      6      6
Blake     7      6      5

DF1/DF2 = DF3

DF3 (result)
Name    Jan    Feb    Mar
Aaron   0.5    0.7    0.5
Blake   0.7    1.0    0.8

我正在使用子集,然后使用dcast构建每个数据帧,但是很难弄清楚如何划分它们.感谢您的帮助!

I'm using subset then dcast to build each data frame, but having a hard time figuring out how to divide them. Thanks for your help!

推荐答案

我们将'DF1'和'DF2'中的数字列相除(通过删除第一列),然后将 cbind 与第一列列.

We divide the numeric columns in both 'DF1' and 'DF2' (by removing the first column) and cbind with the first column.

DF3 <- cbind(DF1[1],round(DF1[-1]/DF2[-1],1))
DF3
#    Name Jan Feb Mar
# 1 Aaron 0.5 0.7 0.5
# 2 Blake 0.7 1.0 0.8

这篇关于在R中划分两个数据帧(一个到另一个)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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