首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将每4行转成4列

将每4行转成4列
EN

Stack Overflow用户
提问于 2019-02-17 05:35:40
回答 2查看 111关注 0票数 2

我试图使用以下循环从IMDB中刮取日期、标题和评论:

代码语言:javascript
复制
   library(rvest)
   library(dplyr)
   library(stringr)
   library(tidyverse)

   ID <- 4633694

data <- lapply(paste0('http://www.imdb.com/title/tt', ID, '/reviews?filter=prolific', 1:20),
                   function(url){
                     url %>% read_html() %>% 
                       html_nodes(".review-date,.rating-other-user-rating,.title,.show-more__control") %>% 
                       html_text() %>%
                       gsub('[\r\n\t]', '', .)
                   })

它以下列格式提供20页的审阅数据,重复相同的模式:

代码语言:javascript
复制
   col1
1 10/10
2 If this was..
3 14 December 2018
4 I have to say, and no...
5
6
7 10/10
8 Stan Lee Is Smiling Right Now...
9 17 December 2018
10 A movie worthy of...
11
12
13 10/10
14 the most visually stunning film I've ever seen...
15 20 December 2018
16 There's hardly anything... 
17.
18.

我想知道是否有一种方法可以将每4行转换为单个列,以便每个属性在适当的列中对齐,如下所示:

代码语言:javascript
复制
         Date          Rating     Title            Review
1. 14 December 2018    10/10    If this was..    I have to...
2. 17 December 2018    10/10   Stan Lee Is...    A movie worthy...
3. 20 December 2018    10/10  the most visually.. There's hardly anything...
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-02-17 08:08:18

代码语言:javascript
复制
text_data = gsub('\\b(\\d+/\\d+)\\b','\n\\1',paste(grep('\\w',x$col1,value = TRUE),collapse = ':')) 

read.csv(text=text_data,h=F,sep=":",strip.white = T,fill=T,stringsAsFactors = F)
     V1                                                V2               V3                         V4 V5
1 10/10                                     If this was.. 14 December 2018   I have to say, and no... NA
2 10/10                  Stan Lee Is Smiling Right Now... 17 December 2018       A movie worthy of... NA
3 10/10 the most visually stunning film I've ever seen... 20 December 2018 There's hardly anything... NA
票数 3
EN

Stack Overflow用户

发布于 2019-02-17 05:59:45

这里有一个方法。

数据:

代码语言:javascript
复制
x <- read.csv2(header=TRUE, stringsAsFactors=FALSE, text="
col1
10/10
If this was..
14 December 2018
I have to say, and no...


10/10
Stan Lee Is Smiling Right Now...
17 December 2018
A movie worthy of...


10/10
the most visually stunning film I've ever seen...
20 December 2018
There's hardly anything... 
.
.")

首先,我们“找到”每一行顶部,在这种情况下,它看起来像一个日期。请注意,您可能希望/需要微调这个正则表达式,将假阳性和假阴性最小化。

代码语言:javascript
复制
ind <- grep("^[0-9]+/[0-9]+", x$col1)
x$col1[ind]
# [1] "10/10" "10/10" "10/10"

底线是将每个块的第一行的索引放入ind中。

从这里开始,让我们将每个块提取到下一个块开始的位置(减去1),直到框架列的末尾:

代码语言:javascript
复制
y <- Map(function(a,b) x$col[a:b], ind, c(ind[-1], nrow(x)))
str(y)
# List of 3
#  $ : chr [1:5] "10/10" "If this was.." "14 December 2018" "I have to say, and no..." ...
#  $ : chr [1:5] "10/10" "Stan Lee Is Smiling Right Now..." "17 December 2018" "A movie worthy of..." ...
#  $ : chr [1:6] "10/10" "the most visually stunning film I've ever seen..." "20 December 2018" "There's hardly anything... " ...

我们可以尝试跳到前面(下面的do.call ),但是它会遇到问题,因为我们的向量有不同的大小。通过将它们的长度设置为最长向量的长度,我们可以很容易地解决这个问题。这里有个技巧可以做到:

代码语言:javascript
复制
z <- lapply(y, `length<-`, max(lengths(y)))
str(z)
# List of 3
#  $ : chr [1:6] "10/10" "If this was.." "14 December 2018" "I have to say, and no..." ...
#  $ : chr [1:6] "10/10" "Stan Lee Is Smiling Right Now..." "17 December 2018" "A movie worthy of..." ...
#  $ : chr [1:6] "10/10" "the most visually stunning film I've ever seen..." "20 December 2018" "There's hardly anything... " ...

最后一步:

代码语言:javascript
复制
setNames(do.call("rbind.data.frame", c(z, stringsAsFactors=FALSE)),
         letters[seq_len(length(z[[1]]))])
#       a                                                 b                c
# 1 10/10                                     If this was.. 14 December 2018
# 2 10/10                  Stan Lee Is Smiling Right Now... 17 December 2018
# 3 10/10 the most visually stunning film I've ever seen... 20 December 2018
#                             d     e    f
# 1    I have to say, and no... 10/10 <NA>
# 2        A movie worthy of... 10/10 <NA>
# 3 There's hardly anything...      .    .
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54730453

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档