Чтение файла с одним столбцом со строками в качестве имен переменных - PullRequest
0 голосов
/ 06 ноября 2018

Я пытаюсь работать с некоторым анализом настроений, но, к сожалению, застрял в самом начале, я даже не могу импортировать файл.

Данные находятся здесь: http://snap.stanford.edu/data/web-FineFoods.html

Это 353 МБ .txt файл, который выглядит следующим образом:

product/productId: B001E4KFG0 review/userId: A3SGXH7AUHU8GW review/profileName: delmartian review/helpfulness: 1/1 review/score: 5.0 review/time: 1303862400 review/summary: Good Quality Dog Food review/text: I have bought several of the Vitality canned dog food products and have found them all to be of good quality. The product looks more like a stew than a processed meat and it smells better. My Labrador is finicky and she appreciates this product better than most.

Все мои попытки выбросили эти данные в один столбец, и я не уверен, как мне правильно сортировать их, чтобы обработать их в текстовом формате.

Я был бы рад колонкам с заголовками, показанными в каждой строке здесь.

Цените любое направление.

1 Ответ

0 голосов
/ 06 ноября 2018

Вот один из способов сделать это с dplyr и tidyr -

# assuming your data is in file called reviews.txt
reviews <- readLines("reviews.txt")

df <- data_frame(chars = trimws(reviews)) %>%
  mutate(
    variable_num = cumsum(grepl(":", chars))
  ) %>%
  group_by(variable_num) %>%
  summarise(
    chars = paste0(chars, collapse = " ")
  ) %>%
  separate(chars, into = c("variable", "value"), sep = ": ", extra = "merge") %>%
  select(-variable_num) %>% 
  mutate(
    variable = sub(".*/", "", variable),
    record_num = cumsum(variable == "productId")
  ) %>% 
  spread(variable, value, convert = T)


> df
  record_num helpfulness productId  profileName score summary  text          time userId
       <int> <chr>       <chr>      <chr>       <dbl> <chr>    <chr>        <int> <chr> 
1          1 1/1         B001E4KFG0 delmartian      5 Good Qu~ "I have bo~ 1.30e9 A3SGX~
...