R语言报错:UseMethod('select')错误及ggplot对象'viz'未找到
RStudio问题排查:dplyr select失效与ggplot对象未找到错误
问题现象
- 代码上半部分的
select功能昨日正常,今日失效,数据未按预期筛选处理 - 代码下半部分ggplot报错:
Error in ggplot(data = viz, aes(x = group, y = insured)) : 找不到对象'viz'
用户代码
# pulling libraries library('dplyr') library('ggplot2') # reading the data data <- read.csv('week2_dataset.csv') #looking at the data data #viewing the data in a more pretty way View(data) #selecting variables data %>% #selecting the variable, group, total, and insured columns select (variable, group, total, insured, uninsured) %>% #filtering by race age and sex, my 3 picked variables filter(variable %in% c('race', 'age', 'sex')) %>% mutate(percent_insured = insured / total) %>% mutate(percent_uninsured = uninsured / total) %>% mutate(least_insured = ifelse (percent_insured <0.9, 1, 0)) %>% mutate (most_insured = ifelse (percent_insured >0.9, 1, 0)) %>% mutate (most_least = ifelse (least_insured <0.9, "most", "least")) print(data) data ggplot(data = viz, aes (x = group, y = insured)) + geom_col() + geom_text(vjust = -1)
调试数据
dput(head(data)) structure(c("function (..., list = character(), package = NULL, lib.loc = NULL, ", " verbose = getOption(\"verbose\"), envir = .GlobalEnv, overwrite = TRUE) ", "{", " fileExt <- function(x) {", " db <- grepl(\"\\\\.[^.]+\\\\.(gz|bz2|xz)$\", x)", " ans <- sub(\".*\\\\.\", \"\", x)"), dim = c(6L, 1L), dimnames = list( c("1", "2", "3", "4", "5", "6"), ""), class = "noquote") >
问题原因与解决方法
1. select功能失效的原因
- 管道操作
data %>% ...的结果没有赋值回任何变量,原data完全未被修改,处理结果仅临时输出,未保存。 - 从调试数据看,
data被意外覆盖成了函数代码,大概率是之前误操作导致(比如将某个函数赋值给了data变量)。
解决步骤:
- 先清除错误的
data对象:rm(data) - 重新读取数据,将管道处理结果赋值回
data或新变量:
data <- data %>% select(variable, group, total, insured, uninsured) %>% filter(variable %in% c('race', 'age', 'sex')) %>% mutate(percent_insured = insured / total, percent_uninsured = uninsured / total, least_insured = ifelse(percent_insured < 0.9, 1, 0), most_insured = ifelse(percent_insured > 0.9, 1, 0), # 修正most_least逻辑:原判断中least_insured是0/1,永远不会<0.9,会导致列值全为"least" most_least = ifelse(percent_insured > 0.9, "most", "least"))
2. ggplot对象未找到的原因
- ggplot中指定了
data = viz,但代码从未创建过viz对象,应使用处理后的数据集(比如修改后的data)。 geom_text缺少label参数,会导致后续报错,需补充要显示的文本内容。
修正后的ggplot代码:
ggplot(data = data, aes(x = group, y = insured)) + geom_col() + geom_text(aes(label = insured), vjust = -1)
完整修正代码
# 加载库 library(dplyr) library(ggplot2) # 清除错误数据对象(如果存在) if(exists("data")) rm(data) # 读取数据 data <- read.csv('week2_dataset.csv') # 查看数据结构 View(data) # 数据处理并保存结果 data <- data %>% select(variable, group, total, insured, uninsured) %>% filter(variable %in% c('race', 'age', 'sex')) %>% mutate(percent_insured = insured / total, percent_uninsured = uninsured / total, least_insured = ifelse(percent_insured < 0.9, 1, 0), most_insured = ifelse(percent_insured > 0.9, 1, 0), most_least = ifelse(percent_insured > 0.9, "most", "least")) # 打印处理后的数据 print(data) # 绘制柱状图 ggplot(data = data, aes(x = group, y = insured)) + geom_col() + geom_text(aes(label = insured), vjust = -1)
内容的提问来源于stack exchange,提问作者Ashings Total
相关产品推荐
相关产品推荐

