2017-09-21 107 views
0

我正在開發一個R項目。我使用的數據集在以下鏈接處可用 https://www.kaggle.com/ranjitha1/hotel-reviews-city-chennai/data如何解決以下錯誤?輸入必須是任意長度的字符向量或字符向量列表,其中每個長度都爲1.

我已經使用的代碼是。

df1 = read.csv("chennai.csv", header = TRUE) 
library(tidytext) 
tidy_books <- df1 %>% unnest_tokens(word,Review_Text) 

這裏Review_Text是文本列。但是,我收到以下錯誤。

Error in check_input(x) : 
Input must be a character vector of any length or a list of character 
vectors, each of which has a length of 1. 
+1

你需要'stringsAsFactors = FALSE'在'read.csv'聲明。或者使用'read_csv',因爲你似乎在進行全面的工作。 –

+0

我正要說的是,但以更緊湊的方式。考慮在你使用之前檢查新數據的結構,即'str(df1)',這也會提醒你這個問題以及 – Visser

回答

6

stringsAsFactors再次襲擊!

您的Review_Text列是一個因素,而不是字符向量,因爲錯誤消息表示函數需要。

我強烈建議使用readr::read_csv而不是默認的read.csv,因爲它更快,並且其默認值不會導致此問題。否則,只設置stringsAsFactorsFALSE,你是好:

> tidytext::unnest_tokens(readr::read_csv("chennai_reviews.csv"), word, Review_Text) 
Parsed with column specification: 
cols(
    Hotel_name = col_character(), 
    Review_Title = col_character(), 
    Review_Text = col_character(), 
    Sentiment = col_character(), 
    Rating_Percentage = col_character(), 
    X6 = col_integer(), 
    X7 = col_integer(), 
    X8 = col_character(), 
    X9 = col_character() 
) 
Warning: 1 parsing failure. 
row # A tibble: 1 x 5 col  row col expected                          actual expected <int> <chr>  <chr>                          <chr> actual 1 2262 X7 an integer "Expedia Booking availability was , only for Non- AC ; ON REQUEST OVER PHONE got it.\n\nRecommended" file # ... with 1 more variables: file <chr> 

# A tibble: 179,883 x 9 
      Hotel_name       Review_Title Sentiment Rating_Percentage X6 X7 X8 X9  word 
       <chr>         <chr>  <chr>    <chr> <int> <int> <chr> <chr>  <chr> 
1 Accord Metropolitan Excellent comfortableness during stay   3    100 NA NA <NA> <NA>  its 
2 Accord Metropolitan Excellent comfortableness during stay   3    100 NA NA <NA> <NA>  really 
3 Accord Metropolitan Excellent comfortableness during stay   3    100 NA NA <NA> <NA>  nice 
4 Accord Metropolitan Excellent comfortableness during stay   3    100 NA NA <NA> <NA>  place 
5 Accord Metropolitan Excellent comfortableness during stay   3    100 NA NA <NA> <NA>   to 
6 Accord Metropolitan Excellent comfortableness during stay   3    100 NA NA <NA> <NA>  stay 
7 Accord Metropolitan Excellent comfortableness during stay   3    100 NA NA <NA> <NA> especially 
8 Accord Metropolitan Excellent comfortableness during stay   3    100 NA NA <NA> <NA>  for 
9 Accord Metropolitan Excellent comfortableness during stay   3    100 NA NA <NA> <NA> business 
10 Accord Metropolitan Excellent comfortableness during stay   3    100 NA NA <NA> <NA>  and 
# ... with 179,873 more rows 
Warning message: 
Missing column names filled in: 'X6' [6], 'X7' [7], 'X8' [8], 'X9' [9] 

> tidytext::unnest_tokens(read.csv("chennai_reviews.csv", stringsAsFactors = FALSE), word, Review_Text) 
               Hotel_name 
1          Accord Metropolitan 
                                                                 Review_Title 
...snip... 
+1

謝謝!有效 –

相關問題