grepl と grep
正規表現の基本的な使い方として、文字列や文字列ベクトルの中にパターンが存在するかどうかを調べることができます。そのために使う関数が以下の2つです。
どちらの関数も pattern と x という引数を受け取ります。pattern はマッチさせたい正規表現、x は検索対象の文字ベクトルです。
この演習と続く演習では、メールアドレスの文字ベクトルを操作していきます。ベクトル emails はあらかじめ定義されているので、すぐに指示に取り組めます。
この演習はコースの一部です
中級 R
演習の手順
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# The emails vector has already been defined for you
emails <- c("[email protected]", "[email protected]", "[email protected]",
"invalid.edu", "[email protected]", "[email protected]")
# Use grepl() to match for "edu"
# Use grep() to match for "edu", save result to hits
# Subset emails using hits