gpt4 book ai didi

r - 在 R 中循环变量名

转载 作者:行者123 更新时间:2023-12-04 10:14:14 24 4
gpt4 key购买 nike

我有一个循环问题。解决起来应该很简单,但是“R for Stata Users”(我已经在 Stata 中编码了几年)、Roger Peng 的视频和 Google 似乎并没有帮助我。你们中的一个人可以向我解释我做错了什么吗?

我正在尝试编写一个循环,通过“阈值”数据框从三组列中提取信息。同一段代码写3遍就可以做我想做的事,但是随着代码越来越复杂,这会变得相当繁琐。

这是“阈值”的示例(请参阅下面的 dput 输出,由友好的读者添加):

    threshold_1_name      threshold_1_dir threshold_1_value
1 overweight > 25
2 possible malnutrition < 31
3 Q1 > 998
4 Q1 > 998
5 Q1 > 998
6 Q1 > 998
threshold_1_units threshold_2_name threshold_2_dir threshold_2_value threshold_2_units
1 kg/m^2 obese > 30 kg/m^2
2 cm <NA> > NA
3 <NA> Q3 > 998
4 Q3 > 998
5 Q3 > 998
6 Q3 > 998

这段代码做我想做的事:
newvars1 <- paste(thresholds$varname, thresholds$threshold_1_name, sep = "_")
noval <- is.na(thresholds$threshold_1_value)
newvars1 <- newvars1[!noval]

newvars2 <- paste(thresholds$varname, thresholds$threshold_2_name, sep = "_")
noval <- is.na(thresholds$threshold_2_value)
newvars2 <- newvars2[!noval]

newvars3 <- paste(thresholds$varname, thresholds$threshold_3_name, sep = "_")
noval <- is.na(thresholds$threshold_3_value)
newvars3 <- newvars3[!noval]

这是我尝试循环的方式:
variables <- NULL
for (i in 1:3) {
valuevar <- paste("threshold", i, "value", sep = "_")
namevar <- paste("threshold", i, "name", sep = "_")
newvar <- paste("varnames", i, sep = "")
for (j in 1:length(thresholds$varname)) {
check <- is.na(thresholds[valuevar[j]])
if (check == FALSE) {
newvars <- paste(thresholds$varname, thresholds[namevar], sep = "_")
}
}
variables <- c(variables, newvars)
}

这是我收到的错误:
Error: unexpected '}' in "}"

我认为我称“我”的方式有些困惑,但我不确定如何正确地做到这一点。当我切换到 R 时,我使用本地人的 Stata 习惯真的让我很恼火。

编辑添加 dput输出,由一位友好的读者提供:
thresholds <- structure(list(varname = structure(1:6, .Label = c("varA", "varB", 
"varC", "varD", "varE", "varF"), class = "factor"), threshold_1_name = c("overweight",
"possible malnutrition", "Q1", "Q1", "Q1", "Q1"), threshold_1_dir = c(">",
"<", ">", ">", ">", ">"), threshold_1_value = c(25L, 31L, 998L,
998L, 998L, 998L), threshold_1_units = c("kg/m^2", "cm", NA,
NA, NA, NA), threshold_2_name = c("obese", "<NA>", "Q3", "Q3",
"Q3", "Q3"), threshold_2_dir = c(">", ">", ">", ">", ">", ">"
), threshold_2_value = c(30L, NA, 998L, 998L, 998L, 998L), threshold_2_units = c("kg/m^2",
"cm", NA, NA, NA, NA)), .Names = c("varname", "threshold_1_name",
"threshold_1_dir", "threshold_1_value", "threshold_1_units",
"threshold_2_name", "threshold_2_dir", "threshold_2_value", "threshold_2_units"
), row.names = c(NA, -6L), class = "data.frame")

最佳答案

我看到的第一个问题是在 if(check = "FALSE")这是一个任务 =如果你正在测试一个条件,它需要是 == .另外,引用这个词 "FALSE"意味着您正在测试字符串值的变量(字面意思是 FALSE),而不是逻辑值,即 FALSE没有引号。

@BlueMagister 正确地指出了第二个问题,你错过了 )for (j in 1:length(...)) {
见#糟糕!

  for (j in 1:length(thresholds$varname)) { 
check <- is.na(thresholds[valuevar[j]])
if (check = "FALSE") { # bad!
newvars <- paste(thresholds$varname, thresholds[namevar], sep = "_")
}
}

见#好!
  for (j in 1:length(thresholds$varname)) { 
check <- is.na(thresholds[valuevar[j]])
if (check == FALSE) { # good!
newvars <- paste(thresholds$varname, thresholds[namevar], sep = "_")
}
}

但是因为它是一个 if 语句,所以您可以使用非常简单的逻辑,尤其是逻辑(TRUE/FALSE 值)。

看到#更好!
  for (j in 1:length(thresholds$varname)) { 
check <- is.na(thresholds[valuevar[j]])
if (!check) { # better!
newvars <- paste(thresholds$varname, thresholds[namevar], sep = "_")
}
}

关于r - 在 R 中循环变量名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13997298/

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