library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.2 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.3 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
text <- "Hello, this is an example text. 20"
# Kiểm tra xem chuỗi có chứa từ "example" hay không
contains_example <- str_detect(text, "example")
print(contains_example) # Kết quả: TRUE
## [1] TRUE
# Trích xuất các con số từ chuỗi
numbers <- str_extract(text, "\\d+")
print(numbers) # Kết quả: NA (không có con số trong chuỗi)
## [1] "20"
# Thay thế từ "example" bằng từ "illustration"
modified_text <- str_replace(text, "example", "illustration")
print(modified_text) # Kết quả: "Hello, this is an illustration text."
## [1] "Hello, this is an illustration text. 20"
# Chia chuỗi thành các từ riêng biệt
split_words <- str_split(text, "\\s")
print(split_words) # Kết quả: List of 7
## [[1]]
## [1] "Hello," "this" "is" "an" "example" "text." "20"
# Đếm độ dài của mỗi từ trong chuỗi
word_lengths <- str_length(split_words[[1]])
print(word_lengths) # Kết quả: 5 4 2 3 4 6 4
## [1] 6 4 2 2 7 5 2
# Đếm số lần xuất hiện của chữ "is" trong chuỗi
is_count <- str_count(text, "is")
print(is_count) # Kết quả: 2
## [1] 2
# Chuyển đổi chuỗi thành chữ in hoa
uppercase_text <- str_to_upper(text)
print(uppercase_text) # Kết quả: "HELLO, THIS IS AN EXAMPLE TEXT."
## [1] "HELLO, THIS IS AN EXAMPLE TEXT. 20"
# Chuyển đổi chuỗi thành chữ thường
lowercase_text <- str_to_lower(text)
print(lowercase_text) # Kết quả: "hello, this is an example text."
## [1] "hello, this is an example text. 20"
# Thay thế tất cả dấu phẩy bằng dấu chấm phẩy
comma_replaced <- str_replace_all(text, ",", ";")
print(comma_replaced) # Kết quả: "Hello; this is an example text."
## [1] "Hello; this is an example text. 20"
# Trích xuất phần đầu của chuỗi
substring <- str_sub(text, start = 1, end = 5)
print(substring) # Kết quả: "Hello"
## [1] "Hello"