기본 콘텐츠로 건너뛰기

4.4.2 R 문자열(TEXT) 다루기 2편

Ch5. 문자열 데이터 다루기 2

Ch5. 문자열 데이터 다루기 2

데이터 다운로드 링크: https://www.kaggle.com/PromptCloudHQ/imdb-data

# 데이터 불러오기
DATA=read.csv("C:\\R/IMDB-Movie-Data.csv")

Genre 변수에서 대표 장르만 뽑아내기

다시 Genre 변수로 돌아오도록 하겠습니다. 하나의 영화는 여러개의 Genre에 해당되지만, 일반적으로 가장 대표적인 Genre를 맨 앞에 표기합니다. 그러므로 각 영화의 대표장르를 나타내는 변수를 한번 만들어보도록 하겠습니다.

# 첫번째 영화의 장르만 가져와서 문자열 분리 실행
strsplit(as.character(DATA$Genre[1]),',')
## [[1]]
## [1] "Action"    "Adventure" "Sci-Fi"

strsplit 데이터를 쓰면 결과값은 2차원 리스트로 나오게 됩니다. 그러므로 Index또한 2차원 리스트에 맞게 설정을 해주어야합니다. 어려울 수 있지만 이 2개만 확인하면 Index구분은 어렵지 않습니다.

  • [[1]] -> 2차원 리스트 중 첫번째 리스트임을 의미
  • [1] -> 2차원 리스트 중 첫번째 리스트 안의 첫번째 값을 의미
  • [[1]]은 strsplit 함수의 출력결과인 c('Action', “Adventure”, “Sci-Fi”) 를 의미.
  • [1]은 [[1]]안에서의 첫번째 값인 'Action'을 나타냄. [2]인 경우 “Adventure”
strsplit(as.character(DATA$Genre[1]),',')[[1]][1] # Action
## [1] "Action"
strsplit(as.character(DATA$Genre[1]),',')[[1]][2] # Adventure
## [1] "Adventure"
strsplit(as.character(DATA$Genre[1]),',')[[1]][3] # Sci-Fi
## [1] "Sci-Fi"
# 모든 영화의 장르를 가져와서 문자열 분리 실행
GENRE = strsplit(as.character(DATA$Genre),',')
GENRE[[123]] # 123번째 영화의 Genre
## [1] "Adventure" "Comedy"    "Romance"
GENRE[[123]][1]  # 123번째 영화의 대표 Genre
## [1] "Adventure"

이제 Index개념에 대해 조금은 익숙해 지셨나요? 이제 이 대표장르들을 하나씩 저장해주는 변수를 만들 것입니다.

사용 함수는 for문을 사용할 것입니다. for문으로 변수를 만들 때는 다음과 같은 순서로 진행하면 됩니다.

  1. 저장공간 생성
  2. for문 연산 범위 생성
  3. 인덱스에 맞춰 저장 실행
Rep_Genre = c( ) # 저장공간 생성
for(i in 1: 1000){ # for문 연산 범위 설정
  Rep_Genre[i] = GENRE[[i]][1] # Rep_Genre에 대표 Genre 차례대로 저장
}
Rep_Genre[1:100]
##   [1] "Action"    "Adventure" "Horror"    "Animation" "Action"   
##   [6] "Action"    "Comedy"    "Comedy"    "Action"    "Adventure"
##  [11] "Adventure" "Biography" "Action"    "Animation" "Action"   
##  [16] "Animation" "Biography" "Action"    "Biography" "Drama"    
##  [21] "Adventure" "Drama"     "Crime"     "Animation" "Action"   
##  [26] "Comedy"    "Action"    "Horror"    "Comedy"    "Action"   
##  [31] "Comedy"    "Drama"     "Action"    "Action"    "Action"   
##  [36] "Action"    "Adventure" "Action"    "Action"    "Comedy"   
##  [41] "Animation" "Drama"     "Horror"    "Biography" "Drama"    
##  [46] "Action"    "Drama"     "Adventure" "Action"    "Drama"    
##  [51] "Action"    "Action"    "Comedy"    "Action"    "Action"   
##  [56] "Adventure" "Crime"     "Drama"     "Comedy"    "Biography"
##  [61] "Action"    "Horror"    "Crime"     "Drama"     "Drama"    
##  [66] "Action"    "Drama"     "Action"    "Drama"     "Action"   
##  [71] "Drama"     "Action"    "Drama"     "Drama"     "Animation"
##  [76] "Action"    "Action"    "Adventure" "Action"    "Action"   
##  [81] "Action"    "Comedy"    "Biography" "Crime"     "Action"   
##  [86] "Action"    "Crime"     "Action"    "Crime"     "Action"   
##  [91] "Crime"     "Action"    "Drama"     "Comedy"    "Action"   
##  [96] "Action"    "Animation" "Horror"    "Drama"     "Crime"
DATA$Rep_Genre = Rep_Genre # Rep_Genre 변수 생성

이렇게하면 Rep_Genre에 1000개 영화의 대표장르가 순서대로 저장된 것을 확인할 수 있습니다.

# 한번에 하고 싶을 경우 
Rep_Genre = c( )
for(i in 1:1000){
  Rep_Genre[i] = strsplit(as.character(DATA$Genre),',')[[i]][1]
}
Rep_Genre[1:100]
##   [1] "Action"    "Adventure" "Horror"    "Animation" "Action"   
##   [6] "Action"    "Comedy"    "Comedy"    "Action"    "Adventure"
##  [11] "Adventure" "Biography" "Action"    "Animation" "Action"   
##  [16] "Animation" "Biography" "Action"    "Biography" "Drama"    
##  [21] "Adventure" "Drama"     "Crime"     "Animation" "Action"   
##  [26] "Comedy"    "Action"    "Horror"    "Comedy"    "Action"   
##  [31] "Comedy"    "Drama"     "Action"    "Action"    "Action"   
##  [36] "Action"    "Adventure" "Action"    "Action"    "Comedy"   
##  [41] "Animation" "Drama"     "Horror"    "Biography" "Drama"    
##  [46] "Action"    "Drama"     "Adventure" "Action"    "Drama"    
##  [51] "Action"    "Action"    "Comedy"    "Action"    "Action"   
##  [56] "Adventure" "Crime"     "Drama"     "Comedy"    "Biography"
##  [61] "Action"    "Horror"    "Crime"     "Drama"     "Drama"    
##  [66] "Action"    "Drama"     "Action"    "Drama"     "Action"   
##  [71] "Drama"     "Action"    "Drama"     "Drama"     "Animation"
##  [76] "Action"    "Action"    "Adventure" "Action"    "Action"   
##  [81] "Action"    "Comedy"    "Biography" "Crime"     "Action"   
##  [86] "Action"    "Crime"     "Action"    "Crime"     "Action"   
##  [91] "Crime"     "Action"    "Drama"     "Comedy"    "Action"   
##  [96] "Action"    "Animation" "Horror"    "Drama"     "Crime"

이렇게 for문과 strsplit 및 R에서 결과값을 뿌려주는 Index를 활용하여 대표 장르를 뽑아낼 수 있습니다.