tastycanofmalk
tastycanofmalk

Reputation: 628

Reading file with one column with rows as variable names

I'm trying to work with some sentiment analysis but unfortunately stuck on the very beginning, I can't even import the file.

The data is located here: http://snap.stanford.edu/data/web-FineFoods.html

It is a 353MB .txt file and and looks like this:

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.

My attempts have all thrown this data into a single column and I'm unsure how I should go about sorting these out correctly in order to process them into tidytext.

I would be happy with columns with the headers shown on each of the rows here.

Appreciate any direction.

Upvotes: 1

Views: 92

Answers (1)

Shree
Shree

Reputation: 11150

Here's one way to do it with dplyr and 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~

Upvotes: 2

Related Questions