读取具有行作为变量名的一列文件

时间:2018-11-06 02:51:49

标签: r read.table tidytext

我正在尝试进行情感分析,但不幸的是,一开始,我什至无法导入文件。

数据位于此处:http://snap.stanford.edu/data/web-FineFoods.html

这是一个353MB的.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 :(得分:2)

这是使用dplyrtidyr的一种方法-

# 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~