创建替代变量的公式的帮助

huangapple go评论107阅读模式
英文:

Assistance in creating formula to replace variables

问题

I have a dataset like this:

  1. db <- structure(list(group = c(1, 1, 1, 2, 2, 2, 2), female = c(1,
  2. 0, 1, 1, 1, 0, 1)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
  3. -7L))

Where female=1 means that the patient is female, female=0 means is male.

Then, I have this formula.
Suppose x=frequency of females in group 1, y=frequency of females in group 2 [which I calculated manually]

  1. formula <- function(x,y){
  2. x <- x/100 #frequency should not be expressed as percentage but :100
  3. y <- y/100
  4. z <- x*(1-x)
  5. t <- y*(1-y)
  6. k <- sum(z,t)
  7. l <- k/2
  8. return((x-y)/sqrt(l))
  9. }

Instead of writing manually the frequency, for example, of females in group 1 (x) and in group 2 (y), I would like to change this function so that I can write something like:
formula(db$female, db$group) and automatically in this formula R calculates the frequency of females and uses it in the formula

英文:

I have a dataset like this:

  1. db <- structure(list(group = c(1, 1, 1, 2, 2, 2, 2), female = c(1,
  2. 0, 1, 1, 1, 0, 1)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
  3. -7L))

Where female=1 means that the patient is female, female=0 means is male.

Then, I have this formula.
Suppose x=frequency of females in group 1, y=frequency of females in group 2 [which I calculated manually]

  1. formula <- function(x,y){
  2. x <- x/100 #frequency should not be expressed as percentage but :100
  3. y <- y/100
  4. z <- x*(1-x)
  5. t <- y*(1-y)
  6. k <- sum(z,t)
  7. l <- k/2
  8. return((x-y)/sqrt(l))
  9. }

Instead of writing manually the frequency, for example, of females in group 1 (x) and in group 2 (y), I would like to change this function so that I can write something like:
formula(db$female, db$group) and automatically in this formula R calculates the frequency of females and uses it in the formula

答案1

得分: 1

根据问题描述,我稍微修改了这个函数,应该按描述的方式工作,前提是分组要么是1要么是2。

  1. formula <- function(input1, input2){
  2. x <- sum(input1[input2 == 1]) / length(input1[input2 == 1])
  3. y <- sum(input1[input2 == 2]) / length(input1[input2 == 2])
  4. x <- x/100 #频率不应该表示为百分比,而是:100
  5. y <- y/100
  6. z <- x*(1-x)
  7. t <- y*(1-y)
  8. k <- sum(z,t)
  9. l <- k/2
  10. return((x-y)/sqrt(l))
  11. }

由于xy出现多次,为了避免混淆,我已将函数的参数更改为input1input2

英文:

Based on the description of the problem, I have slightly modified the function, this should work as described, provided the groups are either 1 or 2.

  1. formula &lt;- function(input1, input2){
  2. x &lt;- sum(input1[input2 == 1]) / length(input1[input2 == 1])
  3. y &lt;- sum(input1[input2 == 2]) / length(input1[input2 == 2])
  4. x &lt;- x/100 #frequency should not be expressed as percentage but :100
  5. y &lt;- y/100
  6. z &lt;- x*(1-x)
  7. t &lt;- y*(1-y)
  8. k &lt;- sum(z,t)
  9. l &lt;- k/2
  10. return((x-y)/sqrt(l))
  11. }

Due to x and y appearing multiple times, and to avoid confusion, I have changed the arguments of the function to input1 and input2.

huangapple
  • 本文由 发表于 2023年6月13日 08:38:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76461058.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定