gpt4 book ai didi

R:尝试在 filter.data.frame 中使用零长度变量名

转载 作者:行者123 更新时间:2023-12-05 02:44:58 31 4
gpt4 key购买 nike

我根本不是 R 专家,但我收到了这个错误:

Error in initialize(...) : attempt to use zero-length variable nameCalls: %>% ... filter.data.frame -> filter_rows -> -> initialize -> .Call

通过日志,我将范围缩小到这行代码:

qb_stats_all <- subset(qb_stats_all, select = -c(num)) %>% filter(player != 'Player')

这是之前的完整代码:

library(data.table)
library(dplyr)
library(ggplot2)
library(gtools)
library(htmltab)
library(jsonlite)
library(stringr)
library(XML)


# Set earliest quarterback draft year
beginning_qb_year <- 1985


# Set most recent quarterback draft year
ending_qb_year <- 2020


# Scrape NFL quarterback career data
url_start <- paste0('https://www.pro-football-reference.com/play-index/psl_finder.cgi?request=1&match=combined&year_min=', beginning_qb_year, '&year_max=2020&season_start=1&season_end=-1&pos%5B%5D=qb&draft_year_min=1936&draft_year_max=2020&draft_slot_min=1&draft_slot_max=500&draft_pick_in_round=pick_overall&conference=any&draft_pos%5B%5D=qb&draft_pos%5B%5D=rb&draft_pos%5B%5D=wr&draft_pos%5B%5D=te&draft_pos%5B%5D=e&draft_pos%5B%5D=t&draft_pos%5B%5D=g&draft_pos%5B%5D=c&draft_pos%5B%5D=ol&draft_pos%5B%5D=dt&draft_pos%5B%5D=de&draft_pos%5B%5D=dl&draft_pos%5B%5D=ilb&draft_pos%5B%5D=olb&draft_pos%5B%5D=lb&draft_pos%5B%5D=cb&draft_pos%5B%5D=s&draft_pos%5B%5D=db&draft_pos%5B%5D=k&draft_pos%5B%5D=p&c5val=1.0&order_by=pass_att&offset=')

offset_seq <- seq(0, 500, 100)
qb_stats_all <- data.frame()

for(url_end in offset_seq){

print(paste0('Scraping from QB #', url_end, '!'))
url <- paste0(url_start, url_end)
data <- htmltab(url, which = 1, rm_nodata_cols = F)
qb_stats_all <- rbind(qb_stats_all, data)

}

rm(url_start, url_end, offset_seq, url, data)

print('Starting to clean career data')
# Clean NFL quarterback career data
colnames(qb_stats_all) <- c('num', 'player', 'start_year', 'end_year', 'draft', 'team', 'league', 'g', 'gs', 'cmp', 'att', 'cmp_pct', 'yds', 'td', 'int', 'td_pct', 'int_pct', 'passing_rate', 'sacked', 'sack_yds', 'yards_per_attempt', 'adj_yards_per_attempt', 'adj_net_yards_per_attempt', 'yds_gained_per_game', 'w', 'l', 't')
print('no error 1')
qb_stats_all <- subset(qb_stats_all, select = -c(num)) %>% filter(player != 'Player')

最佳答案

错误是因为列名具有 NA 值。您可以删除它们。

library(dplyr)
qb_stats_all <- qb_stats_all %>%
select(-num, -(28:32)) %>%
filter(player != 'Player')

或者重命名它们以给它们任何合适的列名。

names(qb_stats_all)[28:32] <- paste0('col', 1:5)
qb_stats_all <- qb_stats_all %>% filter(player != 'Player')

关于R:尝试在 filter.data.frame 中使用零长度变量名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66201985/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com