复制Obs以适合df中的其他Obs-R

时间:2020-01-05 23:24:18

标签: r

在下面的图像中,您可以看到有12个团队对象和6个时间对象。当我将此变量添加到Dataframe中时,它显然不适合并出现错误。有没有一种方法可以复制时间,使其显示两次,在同一场比赛中,名字出现1次,底部出现1次?

enter image description here

library(rvest)
library(dplyr)
library(tm)
library(stringi)
library(readr)


today <- Sys.Date()#+1
today <- gsub("-", "", today, fixed=TRUE)

url <- read_html(paste0('https://classic.sportsbookreview.com/betting-odds/nhl-hockey/?date=', as.character(today)))

gametime <- url %>%
  html_nodes('.eventLine-time .eventLine-book-value') %>%
  html_text()

teams <- url %>%
  html_nodes('.team-name a') %>%
  html_text()

roster <- data.frame(gametime = time, TEAM = teams)

编辑中:gametime = rep(gametime,each = 2)

library(rvest)
library(dplyr)
library(tm)
library(stringi)
library(readr)


today <- Sys.Date()#+1
today <- gsub("-", "", today, fixed=TRUE)

url <- read_html(paste0('https://classic.sportsbookreview.com/betting-odds/nhl-hockey/?date=', as.character(today)))

gametime <- url %>%
  html_nodes('.eventLine-time .eventLine-book-value') %>%
  html_text()

gametime = rep(gametime, each=2)

teams <- url %>%
  html_nodes('.team-name a') %>%
  html_text()

roster <- data.frame(gametime = time, TEAM = teams)

1 个答案:

答案 0 :(得分:1)

rep(gametime, each=2)将复制gametime中的每个元素。