gpt4 book ai didi

r - 使用循环或类似函数包装一个长代码

转载 作者:行者123 更新时间:2023-12-04 08:33:23 26 4
gpt4 key购买 nike

我想知道是否有任何方法可以包装下面的代码以使其更短;我正在考虑使用循环或类似的函数来做到这一点。此代码使用 AgeatDeath 和 Disability 生成一个新变量 (cat)。如果 AgeatDeath 介于 75.6 和 77.1 之间并且 Disability 等于“无智力和发育障碍”,则代码创建值为 75.6-77.1 的 cat 变量。
谢谢,
纳德

IDD <- IDD %>%
mutate(
cat = case_when(
AgeatDeath >= 75.6 &
AgeatDeath < 77.1 &
Disability == 'No Intelectual and Developmental Disabilities' ~ '75.6-77.1',
AgeatDeath >= 74.3 &
AgeatDeath < 75.6 &
Disability == 'No Intelectual and Developmental Disabilities' ~ '74.3-75.6',
AgeatDeath >= 72.5 &
AgeatDeath < 74.3 &
Disability == 'No Intelectual and Developmental Disabilities' ~ '72.5-74.3',
AgeatDeath >= 66.5 &
AgeatDeath < 72.5 &
Disability == 'No Intelectual and Developmental Disabilities' ~ '66.6-72.5',

AgeatDeath >= 64.1 &
AgeatDeath < 71.9 &
Disability == 'Intellectual disability' ~ '64.1-71.9',
AgeatDeath >= 62.3 &
AgeatDeath < 64.1 &
Disability == 'Intellectual disability' ~ '62.3-64.1',
AgeatDeath >= 59.4 &
AgeatDeath < 62.3 &
Disability == 'Intellectual disability' ~ '59.4-62.3',
AgeatDeath >= 50.4 &
AgeatDeath < 59.4 &
Disability == 'Intellectual disability' ~ '50.4-59.4',

AgeatDeath >= 56.47 &
AgeatDeath < 59.1 &
Disability == 'Down syndrome' ~ '56.47-59',
AgeatDeath >= 55.59 &
AgeatDeath < 56.47 &
Disability == 'Down syndrome' ~ '55.59-56.47',
AgeatDeath >= 54.42 &
AgeatDeath < 55.59 &
Disability == 'Down syndrome' ~ '54.42-55.59',
AgeatDeath >= 50.92 &
AgeatDeath < 54.42 &
Disability == 'Down syndrome' ~ '50.92-54.42',

AgeatDeath >= 53.3 &
AgeatDeath < 58.2 &
Disability == 'Cerebral palsy' ~ '53.3-58.2',
AgeatDeath >= 50.6 &
AgeatDeath < 53.3 &
Disability == 'Cerebral palsy' ~ '50.6-53.3',
AgeatDeath >= 48.9 &
AgeatDeath < 50.6 &
Disability == 'Cerebral palsy' ~ '48.9-50.6',
AgeatDeath >= 41.38 &
AgeatDeath < 48.9 &
Disability == 'Cerebral palsy' ~ '41.4-48.9',

AgeatDeath >= 44.2 &
AgeatDeath < 51.1 &
Disability == 'Other rare developmental disabilities' ~ '44.2-51',
AgeatDeath >= 41.6 &
AgeatDeath < 44.2 &
Disability == 'Other rare developmental disabilities' ~ '41.6-44.2',
AgeatDeath >= 30.6 &
AgeatDeath < 38.4 &
Disability == 'Other rare developmental disabilities' ~ '30.6-38.4',
AgeatDeath >= 38.4 &
AgeatDeath < 41.6 &
Disability == 'Other rare developmental disabilities' ~ '38.4-41.6'
)
)

最佳答案

一些子集和函数 cut()可以走很长的路。我将演示的内容不涉及 dplyr .
首先创建一个 emtpy 新变量。我们将使用其余的代码在几行中填充它。

IDD$cat <- NA_character
接下来,创建一个值为 Disability 的列表和相应的切点。我们将遍历此列表。
L <- list(
`No Intelectual and Developmental Disabilities` = c(66.6, 72.5, 74.3, 75.6, 77.1),
`Intellectual disability` = c(50.4, 59.4, 62.3, 64.1, 71.9)
)
您可以填写其余部分。现在,我们将使用循环对 Disability 的每个值进行子集化, 使用 cut()将值拆分为类别并重命名类别。
for (d in names(L)) {
IDD$cat[IDD$Disability == d] <- as.character(
cut(IDD$Ageatdeath,
breaks = L[[d]],
labels = paste(L[[d]][-4], L[[d]][-1], sep = "-"),
include.lowest = TRUE,
right = FALSE))
}
cut()拆分 Ageatdeath基于我们提供给 L 的断点.我们根据断点给它标签。 right = FALSE使每个类别包括下限并排除上限,以及 include.lowest = TRUE确保如果任何值处于上限,则它们被包含在最高类别中。我们使用 as.character()以确保它是一个字符向量而不是一个因素。

关于r - 使用循环或类似函数包装一个长代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64922211/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com