如何计算R中2列之间的比率(使用函数)?

发布于 2025-01-22 14:55:14 字数 540 浏览 3 评论 0原文

我们正在进行一个实验,以检查品牌对产品价值的影响。 为了计算结果,我们首先需要计算2个答案之间的比率。 我们想通过功能而不是手动来执行此操作。 例如

prod1_nobrand prod1_brand prod2_nobrand prod2_brand
1             2           4             8
2             6           5             20

在此处输入图像描述“

我们要为每个比率创建一个新列 例如,

prod1_ratio prod2_ratio
2           2
3           4

任何想法如何做? 谢谢你!

we are running an experiment that checks the effect of brand on product value.
In order to calculate the results, we first need to calculate the ratio between 2 answers to the same product.
We want to do it by a function and not manual.
for example

prod1_nobrand prod1_brand prod2_nobrand prod2_brand
1             2           4             8
2             6           5             20

enter image description here

we want to create a new column for each ratio
for example

prod1_ratio prod2_ratio
2           2
3           4

any idea how to do that?
thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心的憧憬 2025-01-29 14:55:14

简单的基础方法将如下...

yourdataframe$new_ratio_col <- yourdataframe$prod1 / yourdataframe$prod2

The simple base R way to do that would be as follows...

yourdataframe$new_ratio_col <- yourdataframe$prod1 / yourdataframe$prod2
向地狱狂奔 2025-01-29 14:55:14

我不是100%确定我正确理解您的问。但是我的感觉是您应该熟悉dplyr功能。您可以使用突变函数以这种方式创建新列:

library(dplyr)

df %>%
mutate(prod1_ratio = prod1_nobrand  / prod2 prod1_brand)

I'm not 100% sure I'm understanding your ask correctly. But my feeling is that you should familiarize yourself with dplyr functions. You can use the mutate function to create new columns in this way:

library(dplyr)

df %>%
mutate(prod1_ratio = prod1_nobrand  / prod2 prod1_brand)
靖瑶 2025-01-29 14:55:14

一个选项是将其置于长形式,计算比率,然后使用tidyverse旋转到宽形式。

library(tidyverse)

df %>%
  pivot_longer(everything(), names_to = c("num", ".value"), names_sep = "_") %>%
  mutate(ratio = brand / nobrand) %>% 
  select(-ends_with("brand")) %>% 
  group_by(num) %>% 
  mutate(rowid = row_number()) %>% 
  pivot_wider(names_from = "num", values_from = "ratio", names_glue = "{num}_ratio") %>% 
  select(-rowid)

输出

  prod1_ratio prod2_ratio
        <dbl>       <dbl>
1           2           2
2           3           4

数据

df <- structure(list(prod1_nobrand = 1:2, prod1_brand = c(2L, 6L), 
    prod2_nobrand = 4:5, prod2_brand = c(8L, 20L)), class = "data.frame", row.names = c(NA, 
-2L))

One option is the put it into long form, calculate the ratio, then pivot back to wide form with tidyverse.

library(tidyverse)

df %>%
  pivot_longer(everything(), names_to = c("num", ".value"), names_sep = "_") %>%
  mutate(ratio = brand / nobrand) %>% 
  select(-ends_with("brand")) %>% 
  group_by(num) %>% 
  mutate(rowid = row_number()) %>% 
  pivot_wider(names_from = "num", values_from = "ratio", names_glue = "{num}_ratio") %>% 
  select(-rowid)

Output

  prod1_ratio prod2_ratio
        <dbl>       <dbl>
1           2           2
2           3           4

Data

df <- structure(list(prod1_nobrand = 1:2, prod1_brand = c(2L, 6L), 
    prod2_nobrand = 4:5, prod2_brand = c(8L, 20L)), class = "data.frame", row.names = c(NA, 
-2L))
热风软妹 2025-01-29 14:55:14

如果在给定的示例c(false,true)c(true,false)中,可以使用列与列配对。

y <- x[c(FALSE, TRUE)] / x[c(TRUE, FALSE)]
names(y) <- sub("_.*", "_ratio", names(y))
y
#  prod1_ratio prod2_ratio
#1           2           2
#2           3           4

数据:

x <- data.frame(prod1_nobrand = 1:2, prod1_brand = c(2L, 6L),
                prod2_nobrand = 4:5, prod2_brand = c(8L, 20L))

In case the columns are paired like in the given example c(FALSE, TRUE) and c(TRUE, FALSE) could be used to subset and make the division.

y <- x[c(FALSE, TRUE)] / x[c(TRUE, FALSE)]
names(y) <- sub("_.*", "_ratio", names(y))
y
#  prod1_ratio prod2_ratio
#1           2           2
#2           3           4

Data:

x <- data.frame(prod1_nobrand = 1:2, prod1_brand = c(2L, 6L),
                prod2_nobrand = 4:5, prod2_brand = c(8L, 20L))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文