LoginSignup
0
0

More than 1 year has passed since last update.

文字列の部分一致によって条件分岐を作る(R言語)

Last updated at Posted at 2019-08-21
if_mojiretu.r
  if (length(grep(pattern="XX", x=i)) > 0){
    Y <- 500
  } else{
    Y <- 50000
  }
  print(Y)

文字列オブジェクト i の中に文字列”XX"が含まれていたらgrep()は1を返すのでTRUE、含まれていなければgrep()はinteger(0)を返す。integer(0)は整数型のデータに何も格納されていない状態なので、それにlength()を取ると0を返してくれる。

追記。
grepl()を使えば返り値を論理型にできるので、こっちのほうがよい。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0