循环遍历R中的变量名

Str*_*h_R 5 r stata

我有一个循环问题.它应该很容易解决,但"St for Stata用户"(我在Stata编码了几年),Roger Peng的视频和谷歌似乎没有帮助我.你们其中一个可以向我解释一下我做错了什么吗?

我正在尝试编写一个贯穿'thresholds'数据帧的循环来从三组列中提取信息.我可以通过三次编写相同的代码段来做我想做的事情,但随着代码变得越来越复杂,这将变得非常麻烦.

以下是"阈值"的示例(请参阅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  
Run Code Online (Sandbox Code Playgroud)

这段代码做我想做的事:

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]
Run Code Online (Sandbox Code Playgroud)

以下是我试图循环的方式:

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)
}
Run Code Online (Sandbox Code Playgroud)

这是我收到的错误:

Error: unexpected '}' in "}"
Run Code Online (Sandbox Code Playgroud)

我认为我称之为"我"的方式正在搞乱,但我不确定如何正确地做到这一点.当我切换到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")
Run Code Online (Sandbox Code Playgroud)

Bra*_*sen 6

我看到的第一个问题是,如果你正在测试它需要的条件,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 = "_")
    }
  }
Run Code Online (Sandbox Code Playgroud)

看#好!

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

但是因为它是一个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 = "_")
    }
  }
Run Code Online (Sandbox Code Playgroud)


ed8*_*d82 1

显然你的 for 循环中缺少一个括号。您应该考虑使用支持大括号匹配的编辑器来避免此类错误。