将对象的矢量转换为字符串以添加到R中的For循环中的列

问题描述 投票:0回答:2

我想要做的是在游戏日志数据框中创建一个列,将守门员名称从守门员矢量分配到列

有没有办法做到这一点?

我听说过deparse(substitute())但是当我使用它时,它在我的for循环中不起作用

library(XML)

Howard<-'http://naturalstattrick.com/playerreport.php?fromseason=20182019&thruseason=20182019&stype=2&sit=5v5&stdoi=oi&rate=n&v=g&playerid=8470657'


Lehner<-'http://naturalstattrick.com/playerreport.php?fromseason=20182019&thruseason=20182019&stype=2&sit=5v5&stdoi=oi&rate=n&v=g&playerid=8475215'

Binnington<-'http://naturalstattrick.com/playerreport.php?fromseason=20182019&thruseason=20182019&stype=2&sit=5v5&stdoi=oi&rate=n&v=g&playerid=8476412'

Goalies<-c(Howard, Lehner, Binnington)

gamelog<-data.frame()

   for(goalie in Goalies){
   goaliehtml<-readHTMLTable(goalie)
   goaliedata<-goaliehtml[['gamelog']]
   goaliedata$player<-deparse(substitute(goalie))
   gamelog<-rbind(gamelog, goaliedata)
}

我希望goaliedata $ player等于正在进行for循环的守门员

r
2个回答
1
投票

我会采用不同的方法。首先,我将播放器名称和ID存储在列表或数据框中。例如:

player_id <- data.frame(player = c("Howard", "Lehner", "Binnington"), 
                        id = c(8470657, 8475215, 8476412),
                        stringsAsFactors = FALSE)

  player          id
1 Howard     8470657
2 Lehner     8475215
3 Binnington 8476412

接下来,我将编写一个函数,它接受playerand id并从网站返回数据的数据框,并添加了播放器名称列。

我的函数使用rvest库,它提供read_htmlhtml_table,而不是XML。有一个复杂因素:缺少的值由-表示,它将列转换为字符。但并非所有玩家都缺少值,因此这些列是数字的。所以函数将-更改为NA,然后在组合玩家之前将所有值转换为数字。 dplyr库提供mutate功能。

library(rvest)
library(dplyr)

get_player_data <- function(player, id) {
  base_url <- "http://naturalstattrick.com/playerreport.php?fromseason=20182019&thruseason=20182019&stype=2&sit=5v5&stdoi=oi&rate=n&v=g&playerid="
  paste0(base_url, id) %>% 
    read_html() %>% 
    html_table(header = TRUE) %>% 
    .[[1]] %>%
    mutate_at(vars(-starts_with("Game"), -starts_with("Team")), 
              funs(as.numeric(gsub("-", NA, .)))) %>% 
    mutate(player = player)
}

现在我们可以浏览每个玩家+ id。我们可以使用pmap_df库中的purrr而不是循环。这需要每个玩家+ id,将它发送到我们的函数并在最后将输出组合成一个数据框:

library(purrr)
player_data <- pmap_df(player_id, get_player_data)

对于3个示例播放器,这将返回83行和52列的数据框,其中播放器名称位于最后一列。

注意:假设所有玩家数据与3个示例具有相同的形式(52列,由-表示的缺失值)。如果没有,该功能可能会出错。


0
投票

goalie不包含守门员的名字。所以首先你给矢量Goalies也是守门员的名字。

library(XML)

Howard<-'http://naturalstattrick.com/playerreport.php?fromseason=20182019&thruseason=20182019&stype=2&sit=5v5&stdoi=oi&rate=n&v=g&playerid=8470657'

Lehner<-'http://naturalstattrick.com/playerreport.php?fromseason=20182019&thruseason=20182019&stype=2&sit=5v5&stdoi=oi&rate=n&v=g&playerid=8475215'

Binnington<-'http://naturalstattrick.com/playerreport.php?fromseason=20182019&thruseason=20182019&stype=2&sit=5v5&stdoi=oi&rate=n&v=g&playerid=8476412'

Goalies<-c(Howard, Lehner, Binnington)
# give the vector the names of the goalies
names(Goalies) <- c("Howard", "Lehner", "Binnington")

gamelog<-data.frame()

for(i in 1:length(Goalies)) {
  goaliehtml<-readHTMLTable(Goalies[i])
  goaliedata<-goaliehtml[['gamelog']]
  goaliedata$player<-names(Goalies[i])
  gamelog<-rbind(gamelog, goaliedata)
}

这是你想要的?

© www.soinside.com 2019 - 2024. All rights reserved.