extract của tidyr

Regular Expressions nâng cao trong R

Angelo Zehr

Data Journalist

Các hàm đã dùng

  • str_match
  • str_replace
  • str_match_all
  • str_replace_all
  • ...
Regular Expressions nâng cao trong R

Nơi regex và data frame gặp nhau:

extract(
    data,
    col,
    into,
    regex = "([[:alnum:]]+)",
    remove = TRUE,
    convert = FALSE,
    ...
 )
Regular Expressions nâng cao trong R

Các đối số của extract

extract(
    data,
    col,
    into,
    regex = "([[:alnum:]]+)",
    remove = TRUE,
    convert = FALSE,
    ...
 )
  • data

  • col

  • into

  • regex

  • remove

  • convert

Regular Expressions nâng cao trong R

Data frame Movies

Regular Expressions nâng cao trong R

Có thể làm gì với str_match

ảnh chụp màn hình một bảng

screens_per_movie %<>%
  mutate(
    is_3d = str_match(line, "3D")
  )
Regular Expressions nâng cao trong R

Kết quả của str_match trông như thế nào

ảnh chụp màn hình một bảng

screens_per_movie %<>%
  mutate(
    is_3d = str_match(line, "3D")
  )
Regular Expressions nâng cao trong R

str_match chỉ khớp được một thông tin

ảnh chụp màn hình một bảng

Regular Expressions nâng cao trong R

extract làm được điều này cho ta

Regular Expressions nâng cao trong R

extract làm được điều này cho ta

ảnh chụp màn hình một bảng

extract(
  screens_per_movie,
  col = "line",
  into = c("is_3d", "screens"),
  regex = "(3D).*?(\\d+)$",
  remove = FALSE
 )
Regular Expressions nâng cao trong R

Kết quả của extract

ảnh chụp màn hình một bảng

extract(
  screens_per_movie,
  col = "line",
  into = c("is_3d", "screens"),
  regex = "(3D).*?(\\d+)$",
  remove = FALSE
)
Regular Expressions nâng cao trong R

Ayo berlatih!

Regular Expressions nâng cao trong R

Preparing Video For Download...