R에서 str_trim을 사용하는 방법(예제 포함)
R의 stringr 패키지의 str_trim() 함수는 문자열에서 공백을 제거하는 데 사용할 수 있습니다.
이 함수는 다음 구문을 사용합니다.
str_trim(string, side = c(“둘 다”, “왼쪽”, “오른쪽”))
금:
- 문자열: 문자형 벡터
- 패턴: 공백을 제거할 면
다음 예에서는 이 기능을 실제로 사용하는 방법을 보여줍니다.
예 1: 왼쪽의 공백 자르기
다음 코드는 str_trim() 함수를 사용하여 문자열 왼쪽에서 공백을 제거하는 방법을 보여줍니다.
library (stringr)
#create string
my_string <- “Hey there everyone.”
#view string
my_string
[1] “Hey there everyone.”
#create new string with white space removed from left
new_string <- str_trim(my_string, side=" left ")
#view new string
new_string
[1] “Hey there everyone.”
문자열 왼쪽의 모든 공백이 잘렸습니다.
예 2: 오른쪽 공백 자르기
다음 코드는 str_trim() 함수를 사용하여 문자열 오른쪽에서 공백을 제거하는 방법을 보여줍니다.
library (stringr)
#create string
my_string <- “Hey there everyone.”
#view string
my_string
[1] “Hey there everyone.”
#create new string with white space removed from right
new_string <- str_trim(my_string, side=" right ")
#view new string
new_string
[1] “Hey there everyone.”
문자열 오른쪽의 모든 공백이 잘렸습니다.
예 3: 양쪽 공간 절단
다음 코드는 str_trim() 함수를 사용하여 문자열 양쪽에서 공백을 제거하는 방법을 보여줍니다.
library (stringr)
#create string
my_string <- “Hey there everyone.”
#view string
my_string
[1] “Hey there everyone.”
#create new string with white space removed from both sides
new_string <- str_trim(my_string, side=" both ")
#view new string
new_string
[1] “Hey there everyone.”
문자열 양쪽의 모든 공백이 잘렸습니다.
추가 리소스
다음 튜토리얼에서는 R에서 다른 일반적인 작업을 수행하는 방법을 설명합니다.
R에서 str_replace를 사용하는 방법
R에서 str_split을 사용하는 방법
R에서 str_Detect를 사용하는 방법
R에서 str_count를 사용하는 방법