6

ループの問題があります。解決するのは簡単なはずですが、「R for Stata Users」(私は数年間 Stata でコーディングしてきました)、Roger Peng のビデオ、および Google は私を助けてくれないようです。私が間違っていることを説明してもらえますか?

「しきい値」データフレームを実行して 3 つの列セットから情報を引き出すループを作成しようとしています。コードの同じ部分を 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 "}"

「i」の呼び方がおかしいと思いますが、どうすればいいのかわかりません。ローカルを使用する私の Stata の習慣は、私が R に切り替えると、本当に私を悩ませています.

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")
4

3 に答える 3

6

私が最初に目にする問題は、必要な条件をテストしている場合if(check = "FALSE")の割り当てです。また、単語を引用すると、引用符がない論理値ではなく、文字列値 (文字通り FALSE という単語) の変数をテストしていることを意味します。==="FALSE"FALSE

2番目の問題は@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 = "_")
    }
  }
于 2012-12-21T21:33:09.523 に答える
1

for ループには明らかにブラケットがありません。この種のエラーを回避するには、ブレース マッチングをサポートするエディターの使用を検討する必要があります。

于 2012-12-21T21:24:17.360 に答える
0

最も簡単な方法は、目的の非ループ コードの機能を実行する関数を作成することだと思います。dput参考までに、編集からの出力を質問に使用した、そのコードからの出力を次に示します。

> newvars1 <- paste(thresholds$varname, thresholds$threshold_1_name, sep = "_")
> newvars1 <- newvars1[!is.na(thresholds$threshold_1_value)]
> newvars2 <- paste(thresholds$varname, thresholds$threshold_2_name, sep = "_") 
> newvars2 <- newvars2[!is.na(thresholds$threshold_2_value)]
> c(newvars1, newvars2)
 [1] "varA_overweight"            "varB_possible malnutrition"
 [3] "varC_Q1"                    "varD_Q1"                   
 [5] "varE_Q1"                    "varF_Q1"                   
 [7] "varA_obese"                 "varC_Q3"                   
 [9] "varD_Q3"                    "varE_Q3"                   
[11] "varF_Q3"  

その関数は次のようになります。

unlist(lapply(1:2, function(k) {
  newvars <- paste(thresholds$varname, 
                   thresholds[[paste("threshold", k, "name", sep="_")]], sep = "_")
  newvars <- newvars[!is.na(thresholds[[paste("threshold", k, "value", sep="_")]])]
}))
# [1] "varA_overweight"            "varB_possible malnutrition"
# [3] "varC_Q1"                    "varD_Q1"                   
# [5] "varE_Q1"                    "varF_Q1"                   
# [7] "varA_obese"                 "varC_Q3"                   
# [9] "varD_Q3"                    "varE_Q3"                   
#[11] "varF_Q3"  

私はあなたのループで何が起こっているのかを理解しようとしましたが、私には意味をなさないことがたくさんありました。そのようにループする場合は、次のように記述します。

variables <- NULL
for (i in 1:2) {
  valuevar <- paste("threshold", i, "value", sep = "_")
  namevar <- paste("threshold", i, "name", sep = "_")
  newvars <- c()
  for (j in 1:nrow(thresholds)) { 
    if (!is.na(thresholds[[valuevar]][j])) {
      newvars <- c(newvars, paste(thresholds$varname[j], 
                                  thresholds[[namevar]][j], sep = "_"))
    }
  }
  variables <- c(variables, newvars)
}
variables
于 2012-12-21T21:36:45.147 に答える