문자함수
1. nchar : 문자수를 리턴하는 함수
nchar('R Developer',type='chars') : 문자의 수(기본값) ⇒ 11 영어
nchar('R Developer',type = 'bytes') : 문자의 byte ⇒ 11 영어
nchar('빅데이터',type='chars') : 문자의 수(기본값) ⇒ 4 한국어
nchar('빅데이터',type='bytes') : 문자의 byte ⇒ 8 한국어
2. strsplit : 부분문자로 분리하는 함수
strsplit('R Developer',split = ' ') : 공백문자를 기준으로 분리. list형으로 반환
strsplit('R,Developer',split = ',') : 콤마를 기준으로 분리
strsplit('R Developer',split = '') : 한글자씩 분리
strsplit('R Developer',split = character(0)) : 한글자씩 분리
unlist : list자료형을 vector자료형으로 변환하는 함수
unlist(strsplit('R Developer',split = ' '))
문자합치기
y <- c('a','b','c')
paste(y[1],y[2],y[3])
paste0(y[1],y[2],y[3])
paste(y,collapse = '') : 공백문자 없이 vector값 합치기
3. toupper : 대문자로 변환하는 함수 toupper('r developer')
4. tolower : 소문자로 변환하는 함수 tolower('R DEVELOPER')
library(tools)
tools :: toTitleCase(x) : 첫글자 대문자 뒷글자 소문자로 변환하는 함수
5. substr : 문자를 추출하는 함수
substr('원본',시작점,끝점)
substr('R Developer',3,4) : 3번째 글자부터 2글자 뽑으려면 4(첫글자부터 카운팅) 해야함.
substr(x,1,4)
substr(x,2,5)
substr(x,3,6)
⇒ substring(x,1:3,4:6)
substr(x,1,4)
substr(x,2,5)
substr(x,3,4)
⇒ substring(x,1:3,4:5)
substr(x,1,4)
substr(x,2,5)
substr(x,3,4)
substr(x,4,5)
⇒ substring(x,1:4,4:5)
substring(원본,시작점) : 문자를 시작점부터 끝까지 추출하는 함수
6. sub : 첫번째 일치하는 문자만 바꾸는 함수
sub('찾을문자','변환할문자','원본')
sub('R','Python','R Programmer R Developer')
7. gsub : 일치하는 모든 문자를 바꾸는 함수
gsub('찾을문자','변환할문자','원본')
gsub('R','Python','R Programmer R Developer')
[문제44] x변수의 값중에 제일 뒤에 두글자만 추출해주세요. x <- 'developer'
substr(x,nchar(x)-1,nchar(x))
[문제45] last_name의 글자의 수가 10이상인 사원의 employee_id, last_name 출력하세요.
employees[nchar(employees$LAST_NAME) >=10, c('EMPLOYEE_ID','LAST_NAME')]
[문제46] last_name, last_name의 첫번째 철자부터 세번째 철자까지 함께 출력하세요.
paste(employees$LAST_NAME,substr(employees$LAST_NAME,1,3))
[문제47] developer 글자를 첫글자 대문자, 뒷글자는 소문자로 변환하세요.
①
paste0(toupper(substr(x,1,1)),tolower(substr(x,2,nchar(x)))
paste0(toupper(substr(x,1,1)),tolower(substring(x,2)))
②
library(tools)
tools :: toTitleCase(x)
[문제48] last_name, salary값을 화면에 출력할때 0은 * 로 출력하세요.
paste(employees$LAST_NAME,gsub('0','*',employees$SALARY))
[문제49] last_name의 두번째 철자가 m 또는 b 인 사원들의 last_name, salary를 출력하세요.
employees[substr(employees$LAST_NAME,2,2) %in% c('m','b') ,c('LAST_NAME','SALARY')]
[문제50] last_name의 제일 뒷글자만 대문자 앞글자들은 소문자로 출력하세요.
x <- employees$LAST_NAME
paste0(tolower(x,1,nchar(x)-1),toupper(x,nchar(x),nchar(x)))
'R' 카테고리의 다른 글
[R] lubridate 패키지 (0) | 2022.01.13 |
---|---|
[R] 숫자함수, 날짜함수 (0) | 2022.01.13 |
[R] read.csv, 조건문, paste (0) | 2022.01.12 |
[R] 자료형 - 7.data frame (0) | 2022.01.12 |
[R] 자료형 - 6.factor (0) | 2022.01.12 |