根据列/条件的组合添加或减去数字

发布于 2025-02-11 08:12:45 字数 2019 浏览 1 评论 0原文

我想调整HDLldltrig,通过根据虚拟变量列的组合添加或减去常数。常数为:

MEDHDLLDLTRIG
MED1-3.534.520.1
MED2-6.324.70
MED3-542.312
MED40230 22 22
22 MED4222222.2

具体而言,规则是:

  1. 当一个人只服用一种药物/减去相应的常数。例如,对于ID#12,谁只服用Med5:
  • adj_hdl =原始HDL -2.1
  • AXD_LDL =原始LDL + 12
  • AXP_TRIG =原始Trig + 22.2
  1. 当一个人服用超过1种药物时,调整后的HDL/LDL/TRIG将是使用最大的绝对值使用常数添加/取消。

例如,对于ID#1,他/她正在服用Med2,Med3和Med5。然后,他/她的调整后的脂质将是:

  • adj_hdl =原始HDL -6.3(因为在Med2,3,5中,Med2在HDL的所有Meds中具有最大的绝对值)
  • ADJ_LDL =原始LDL + 42.3
  • AXP_TRIG =原始Trig = Original Trig + Original Trig + Original Trig + 22.2

因此,最终产品将是一个具有附加3列的数据集,adj_hdladj_ldladj> AXP_TRIG for to ever of vor aff code> aidk_hdl 。

模拟数据集:

set.seed(100)
id = 1:100
hdl = rnorm(100, mean = 50, sd = 3)
ldl = rnorm(100, mean = 120, sd = 10)
trig = rnorm(100, mean = 150, sd = 12)
med1 = rbinom(100, size = 1, prob = 0.4)
med2 = rbinom(100, size = 1, prob = 0.6)
med3 = rbinom(100, size = 1, prob = 0.55)
med4 = rbinom(100, size = 1, prob = 0.45)
med5 = rbinom(100, size = 1, prob = 0.72)

data = cbind(id, hdl, ldl, trig, med1, med2, med3, med4, med5) 

I would like to adjust hdl, ldl, and trig by adding or subtracting a constant based on the combination of the dummy variable columns. The constants are:

medhdlldltrig
med1-3.534.520.1
med2-6.324.70
med3-542.312
med402322
med5-2.11222.2

Specifically, the rules are:

  1. When a person is only taking one 1 medication, then add/subtract the corresponding constant. For example, For ID#12 who's only taking med5:
  • Adj_hdl = original hdl - 2.1
  • Adj_ldl = original ldl + 12
  • Adj_trig = original trig + 22.2
  1. When a person is taking more than 1 medication, the the adjusted hdl/ldl/trig would be added/substracted using the constant with the largest absolute value .

For example, for ID #1, he/she's taking med2, med3, and med5. Then his/her adjusted lipids would be:

  • Adj_hdl = original hdl - 6.3 (since among med2,3,5, med2 has the constant with the largest absolute value across all meds for hdl)
  • Adj_ldl = original ldl + 42.3
  • Adj_trig = original trig + 22.2

As such, the end product would be a data set with additional 3 columns, adj_hdl, adj_ldl, and adj_trig for each of the IDs.

Mock data set:

set.seed(100)
id = 1:100
hdl = rnorm(100, mean = 50, sd = 3)
ldl = rnorm(100, mean = 120, sd = 10)
trig = rnorm(100, mean = 150, sd = 12)
med1 = rbinom(100, size = 1, prob = 0.4)
med2 = rbinom(100, size = 1, prob = 0.6)
med3 = rbinom(100, size = 1, prob = 0.55)
med4 = rbinom(100, size = 1, prob = 0.45)
med5 = rbinom(100, size = 1, prob = 0.72)

data = cbind(id, hdl, ldl, trig, med1, med2, med3, med4, med5) 

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

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

发布评论

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

评论(1

半葬歌 2025-02-18 08:12:45

这很混乱,但我相信它可以起作用。

library(dplyr)
d <- as.data.frame(data)

df: Comparison dataframe
   med  hdl  ldl trig
1 med1 -3.5 34.5 20.1
2 med2 -6.3 24.7  0.0
3 med3 -5.0 42.3 12.0
4 med4  0.0 23.0 22.0
5 med5 -2.1 12.0 22.2

d |> 
  rowwise() |> 
  mutate(across(ldl:trig, ~ .x + max(df[[cur_column()]][df$med %in% (colnames(cur_data()[5:9])[which(cur_data()[5:9] == 1)])]),
                .names = "adj_{.col}")) |> 
  mutate(adj_hdl = hdl - max(abs(df$hdl[df$med %in% (colnames(cur_data()[5:9])[which(cur_data()[5:9] == 1)])])))

修订 (添加了ifelse而不是第二突变)

d |>  
rowwise() |>  
mutate(across(hdl:trig, ~ { 
adj <- max(abs(df[[cur_column()]][df$med %in% (colnames(cur_data()[5:9 [which(cur_data()[5:9] == 1)])]))     
ifelse(cur_column() == "hdl", .x - adj, .x + adj)   }, .names = "adj_{.col}"))



# A tibble: 100 × 12
# Rowwise: 
      id   hdl   ldl  trig  med1  med2  med3  med4  med5 adj_ldl adj_trig adj_hdl
   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl>   <dbl>
 1     1  48.5  117.  150.     0     1     1     0     1    159.     173.    42.2
 2     2  50.4  134.  146.     0     0     1     0     1    176.     168.    45.4
 3     3  49.8  115.  160.     0     0     1     0     1    158.     182.    44.8
 4     4  52.7  128.  156.     0     1     1     1     1    171.     178.    46.4
 5     5  50.4  105.  162.     0     1     0     1     1    130.     184.    44.1
 6     6  51.0  116.  138.     1     1     1     1     1    158.     160.    44.7
 7     7  48.3  112.  143.     0     1     1     0     1    155.     165.    42.0
 8     8  52.1  116.  138.     0     0     0     1     1    139.     160.    50.0
 9     9  47.5  132.  114.     1     0     1     0     1    175.     136.    42.5
10    10  48.9  119.  154.     0     1     0     0     0    144.     154.    42.6

It's messy but I believe it works.

library(dplyr)
d <- as.data.frame(data)

df: Comparison dataframe
   med  hdl  ldl trig
1 med1 -3.5 34.5 20.1
2 med2 -6.3 24.7  0.0
3 med3 -5.0 42.3 12.0
4 med4  0.0 23.0 22.0
5 med5 -2.1 12.0 22.2

d |> 
  rowwise() |> 
  mutate(across(ldl:trig, ~ .x + max(df[[cur_column()]][df$med %in% (colnames(cur_data()[5:9])[which(cur_data()[5:9] == 1)])]),
                .names = "adj_{.col}")) |> 
  mutate(adj_hdl = hdl - max(abs(df$hdl[df$med %in% (colnames(cur_data()[5:9])[which(cur_data()[5:9] == 1)])])))

Revised (added ifelse instead of second mutate)

d |>  
rowwise() |>  
mutate(across(hdl:trig, ~ { 
adj <- max(abs(df[[cur_column()]][df$med %in% (colnames(cur_data()[5:9 [which(cur_data()[5:9] == 1)])]))     
ifelse(cur_column() == "hdl", .x - adj, .x + adj)   }, .names = "adj_{.col}"))



# A tibble: 100 × 12
# Rowwise: 
      id   hdl   ldl  trig  med1  med2  med3  med4  med5 adj_ldl adj_trig adj_hdl
   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl>   <dbl>
 1     1  48.5  117.  150.     0     1     1     0     1    159.     173.    42.2
 2     2  50.4  134.  146.     0     0     1     0     1    176.     168.    45.4
 3     3  49.8  115.  160.     0     0     1     0     1    158.     182.    44.8
 4     4  52.7  128.  156.     0     1     1     1     1    171.     178.    46.4
 5     5  50.4  105.  162.     0     1     0     1     1    130.     184.    44.1
 6     6  51.0  116.  138.     1     1     1     1     1    158.     160.    44.7
 7     7  48.3  112.  143.     0     1     1     0     1    155.     165.    42.0
 8     8  52.1  116.  138.     0     0     0     1     1    139.     160.    50.0
 9     9  47.5  132.  114.     1     0     1     0     1    175.     136.    42.5
10    10  48.9  119.  154.     0     1     0     0     0    144.     154.    42.6
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文