简体   繁体   English

将每第二行和第三行移动到 r 中的新列中

[英]move every second and third row into new columns in r

I'm scraping the 'ranking' table off of a website.我正在从网站上刮掉“排名”表。 The way the table is structured, the team, grand slam points, and overall points end up in the same column when scraped.表格的结构方式,团队,大满贯积分和总积分在刮擦时最终在同一列中。 I've tried the below, and it looks somewhat correct except the last row.我已经尝试了以下内容,除了最后一行之外,它看起来有些正确。 It took the values of the first row and put them in the last row, but in the wrong place.它取第一行的值并将它们放在最后一行,但放在错误的位置。

I actually have two questions.我其实有两个问题。 The first is, what should I do to fix this?首先是,我应该怎么做才能解决这个问题? The second is, I will be creating a loop to scrape this same table off of over 50 pages from this website.第二个是,我将创建一个循环,从该网站的 50 多个页面中删除同一张表。 Is the structure I have below even acceptable for a loop?我下面的结构是否可以接受循环?

library(rvest)
library(tidyverse)
Url = read_html('http://www.bjjcompsystem.com/tournaments/1869/categories/2053150')

tgo2 = Url %>% 
  html_nodes('td') %>% 
  html_text()

tgo2 = data.frame(tgo2)

t = as.data.frame(matrix(tgo2$tgo2, ncol = 3, byrow = TRUE))

library(rvest)
library(tidyverse)
Url <- read_html("http://www.bjjcompsystem.com/tournaments/1869/categories/2053150")

tgo2 <- Url %>%
  html_nodes("td") %>%
  html_text()

(tgo2 <- tibble(tgo2) |> mutate(
  rn = row_number(),
  grp3 = rn %% 3,
  rgrp = cumsum(grp3 == 1)
))

(result <- pivot_wider(tgo2,
                       names_from = "grp3",
                       values_from = "tgo2") |>
  fill(-rn, .direction = "down") |>
  group_by(rgrp) |>
  filter(rn == max(rn)) |>
  ungroup() |>
  select(-(1:2)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM