youngwoos / Doit_R

<Do it! 쉽게 배우는 R 데이터 분석> 저장소
213 stars 436 forks source link

10-1에 텍스트마이닝의 특수문자제거하기 #37

Closed leesoo86 closed 4 years ago

leesoo86 commented 4 years ago

library(KoNLP) library(dplyr) useNIADic() txt<- readLines("hiphop.txt") head(txt) install.packages("stringr") library(stringr) txt<- str_replace_all(txt,"\W", " ")

이렇게 썼더니

밑에

Error in stri_replace_all_regex(string, pattern, fix_replacement(replacement), : 객체 'txt'를 찾을 수 없습니다 라고

나옵니다. 어떻게 해야 하나요? 감사합니다.

youngwoos commented 4 years ago

"hiphop.txt" 파일이 제대로 불러들여지지 않았을 듯합니다. head(txt)를 실행 했을 때 어떤 결과물이 출력되나요?

leesoo86 commented 4 years ago

txt<- readLines("hiphop.txt") head(txt)에 대한 결과물은 아래와 같이 나왔습니다.

txt<- readLines("hiphop.txt") 경고메시지(들): In readLines("hiphop.txt") : 'hiphop.txt'에서 불완전한 마지막 행이 발견되었습니다 head(txt) [1] "\"보고 싶다" "이렇게 말하니까 더 보고 싶다" "너희 사진을 보고 있어도" "보고 싶다"
[5] "너무 야속한 시간" "나는 우리가 밉다"

책에는 3행으 표시되어 있는데, 결과물에는 1행과 5행으로 나옵니다.

감사합니다.

youngwoos commented 4 years ago

str_replace_all(txt,"\W", " ")에서 \W 부분에 \가 두 개 들어가야 합니다. 아래와 같이 수정해서 다시 시도해보시겠어요? txt <- str_replace_all(txt, "\\W", " ")

leesoo86 commented 4 years ago

그래도 계속" Error in stri_replace_all_regex(string, pattern, fix_replacement(replacement), : 객체 'txt'를 찾을 수 없습니다"라고 계속 나옵니다.

youngwoos commented 4 years ago

코드 실행 결과 캡쳐해서 올려주시겠어요?

leesoo86 commented 4 years ago

책에 나온 순서를 바꿔서 적었더니 해결되었습니다.

책에 있는데로 ,

txt<-readLines("hiphop.txt") head(txt) install.packages("stringr") library(stringr) txt<-str_replace_all(txt,"\W", " ")

순서로 하였더니

Error in stri_replace_all_regex(string, pattern, fix_replacement(replacement), : 객체 'txt'를 찾을 수 없습니다 라는 결과물이 나와서

install.packages("stringr") library(stringr) txt<-readLines("hiphop.txt") head(txt) txt<-str_replace_all(txt,"\W", " ")

위와 같이 하였더니 작동이 되었습니다.

감사합니다.