0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Rのdplyrで完全一致・部分一致

Posted at

完全一致

==


library(dplyr)

df <- data.frame(
        x = c("hoge", "fuga", "bar"), 
        y = c("x", "y", "z")
      )

#      x y
# 1 hoge x
# 2 fuga y
# 3 bar  z

df <- df %>% filter(x=="bar")

#      x y
# 1 bar  z

!=


library(dplyr)

df <- data.frame(
        x = c("hoge", "fuga", "bar"), 
        y = c("x", "y", "z")
      )

#      x y
# 1 hoge x
# 2 fuga y
# 3 bar  z

df <- df %>% filter(x!="bar")

#      x y
# 1 hoge x
# 2 fuga y

部分一致 (like, not like)

like

greplを使う


library(dplyr)

df <- data.frame(
        x = c("hoge", "fuga", "bar"), 
        y = c("x", "y", "z")
      )

#      x y
# 1 hoge x
# 2 fuga y
# 3 bar  z

df <- df %>% filter(grepl("ho", x))

#      x y
# 1 hoge x

not like

!をgreplの前に付けるだけ


library(dplyr)

df <- data.frame(
        x = c("hoge", "fuga", "bar"), 
        y = c("x", "y", "z")
      )

#      x y
# 1 hoge x
# 2 fuga y
# 3 bar  z

df <- df %>% filter(!grepl("ho", x))

#      x y
# 1 fuga y
# 2 bar  z

Tips

複数条件のものを出さないといけない場合、
例えば完全一致かつ、not like な条件抽出など
その場合、条件1つずつコマンド打ってデータフレームとして作成し
次の条件抽出で作成したデータフレームを参照すれば確実

終わりに

ログの解析・集計をするも重すぎてExcelがフリーズするため3年ぶりにRを触りました
色々忘れていたこともありメモとして投稿しました
時間があればRでやったログ集計方法も投稿します

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?