R/R basic

string 문자열 알파벳 세기, 문자열 합치기, 일부 보이기 (str_length, str_c, str_sub)

시키테이 2021. 12. 7. 09:34
반응형

문자열 (스트링, string) 기본인 string 알파벳 글자수 세기, string 합치기, string 일부 보이기 등을 해볼까 합니다. stringr이라는 라이브러리를 가져와야 하는데요. 기본적인 함수 str_length, str_c, str_sub, str_to_lower, str_to_higher 등의 기본함수를 적어봅니다. 

 

name<-c("Rao", "Brian", "Susan", "Ray")
# 이름 4개 정해줍니다. 

str_length(text)
3 5 6 7
# 각 문자열의 알파벳 숫자

str_c("this is", " ", text)
"this is Rao"     "this is Brian"   "this is Canada"  "this is Dodgers"
# 각 문자열에 this is를 붙여줍니다. 

str_c(text, collapse=" ")
"Rao Brian Canada Dodgers"
# 4개의 문자열을 하나로 합쳐버립니다. collapse에 빈칸을 넣어서 하나로 되게 해줍니다. 

str_sub(name, 1, 1)
"R" "B" "S" "R"
# 각 문자열의 첫번째 글자만 뽑아내라.

str_sub(name, -2)
"ao" "an" "an" "ay"
# 각 문자열의 마지막에서 두번째 글자만 뽑아내라. 

str_to_lower(name)
"rao"   "brian" "susan" "ray"  
# 모든 소문자로 

str_to_upper(name)
[1] "RAO"   "BRIAN" "SUSAN" "RAY"  
#모두 대문자로

 

 

 

반응형