본문 바로가기
R

[R] 부분 문자열 추출하기

by Chandler.j 2022. 4. 1.
반응형

fig1. title

R에서 문자열 추출

1. substring

2. 예제

3. 그 박의 문자열 조작 substr, str_sub

4. stringr cheat sheet


1. substring()

substr(text, start, stop)
substring(text, first, last = 1000000L)

2. 예제

 

extracting

substring("HumptyDumpty sat on a wall",5,9)

output of extracting example

replacing

mystring<-"Humpty_Dumpty sat on a wall"
substring(mystring,7,7)=" "
mystring

output of replacing example


3. 그 박의 문자열 조작

 

substr

df1$substring_State = substr(df1$State,1,4)
df1

substr

 

str_sub

library(stringr)
df1$substring_State = str_sub(df1$State,1,4) 
df1

str_sub


4. stringr cheat sheet

 

ref : 

https://stringr.tidyverse.org/

https://github.com/rstudio/cheatsheets/blob/main/strings.pdf

https://www.datasciencemadesimple.com/substring-function-in-r/


TOP

Designed by 티스토리