package :: stringr
개요
문자열 처리 방법, stringi을 기반으로 한 string manipulation functions
특징
1. factor와 character를 같은 방식으로 처리
2. 연관성 있는 함수명과 인수
– stringr의 모든 함수는 str_ 로 시작
– 첫번째 인수는 항상 string 벡터이기 때문에 pipe(%>%) 사용이 쉽다.
– 다른 함수의 입력값으로 사용하기 편리한 출력값. 길이 0인 입력값에 대해 길이 0인 결과를 돌려줌
– 입력값 NA가 포함되어 있을 때는 그 부분의 결과를 NA로 돌려줌
3. 사용빈도가 떨어지는 문자열 조작 처리를 과감하게 제거하여 간략화시킴
Sample String
pattern matching engines
modifier함수를 이용하여, 매칭방법에 활용
fixed() | match exact bytes |
coll() | match human letters |
boundary() | match boundaries |
regex() | |
ignore.case() |
예제>
String Manuplation
str_*(string, …)
stringr | 설명 | Base function |
---|---|---|
str_length | string의 길이 str_length(string) | nchar() |
str_c | 여러 string을 하나의 string으로 Concatenate str_c (str, sep=”, collapse=NULL) – sep은 각 string간 seperator, | paste() paste0() |
str_sub | sting에서 일부substrings 를 Extract string에서 일부substrings 를 Replace str_sub(string, start=1L, end=-1L) | substr() |
str_sub( , start, end)
cf. glue()
Pattern matching
RegEx
https://rstudio.com/wp-content/uploads/2016/09/RegExCheatsheet.pdf
str_* (string, pattern = ” “)
stringr | 설명 | 결과 | Base function |
---|---|---|---|
str_detect | Detect the presence/absence of a pattern in a string. => Keep strings matching a pattern, or find positions 대소문자구분 dd[ str_detect(name,”(?i)korea”),] | T/F | grepl(pattern, x) |
str_subset | wrapper around x[str_detect(x, pattern)] | Vector | grep(pattern, x, value=T) |
str_which | wrapper around str_detect(x, pattern) %>% which() | idx | grep(pattern, x) |
str_count | Count the number of matches in a string. str_length와 비슷하지만, pattern을 줄수 있다. | Vector | |
str_extract str_extract_all | Extract matching patterns from a string | vector | |
str_match str_match_all | Extract matched groups from a string 매치된 부분 문자열을 추출하고 참조를 행렬로 돌려줌. 1열에, str_extract(string, pattern)의 결과를 2열 이후에, 각 괄호에 매치된 이후의 결과를 보여줌 | matrix | |
str_locate str_locate_all | Locate the position of patterns in a string. | start, end | |
str_replace str_replace_all | Replace matched patterns in a string. | sub(pattern, replacement, x) gsub() | |
str_replace_na | Turn NA into “NA” | ||
str_split str_split_fixed | Split up a string into pieces. 최대 n 개의 분할을 지정할 수 있음. | list | strsplit(x, pattern) |
str_view str_view_all | View HTML rendering of regular expression match. |
Formatting (Whitespace)
stringr | 설명 | Base function |
str_pad | Pad a string. 폭을 width 만큼 늘려서 side를 기준으로 공백을 pad에 지정된 문자로 채워넣음 | str_pad(string, width, side=”left”, pad=” “) |
str_trunc | Truncate a character string. 폭을 width 만큼 남기고, side를 기준으로 ellipsis을 채워넣음 | str_trunc(string, width, side = c(“right”, “left”, “center”), ellipsis = “…”) |
str_trim | Trim whitespace from a string. | str_trim(string, side=”left|right|both”) |
str_squish | 공백 제거 | |
str_wrap | 지정한 폭으로 줄바꿈. indent는 선두행의 왼쪽 여백, exdent는 그 이외 행의 왼쪽여백. | str_wrap(string, width=80, indent=0, exdent=0) |
Locale sensitive
stringr | 설명 |
---|---|
str_order str_sort | Order or sort a character vector. |
str_to_upper , str_to_lower , str_to_title | Convert case of a string. |
stringr | 설명 |
---|---|
invert_match | Switch location of matches to location of non-matches. |
str_conv | Specify the encoding of a string. |
str_dup | Duplicate and concatenate strings within a character vector. |
str_glue str_glue_data | Format and interpolate a string with glue |
word | Extract words from a sentence. |