R运行代码提示‘filtered_tg’对象未找到问题咨询
解决R代码中
filtered_tg对象未找到的问题 问题根源
你代码里用到的filter()函数并不是R基础包自带的函数,它属于dplyr(或tidyverse系列)工具包。由于代码未提前加载对应包,R无法识别filter()函数,导致filtered_tg对象根本没被创建出来,自然会触发“对象未找到”的错误。
修复方法
方法1:加载dplyr包后再运行代码
如果第一次使用dplyr,需要先安装包,之后每次使用前加载即可:
# 首次使用执行安装(后续可注释掉) install.packages("dplyr") # 加载dplyr包 library(dplyr) data("ToothGrowth") View(ToothGrowth) filtered_tg <- filter(ToothGrowth, dose == 0.5) View(filtered_tg)
方法2:用R基础语法替代filter()
不想依赖额外包的话,用基础R的subset()函数或直接索引筛选:
用subset函数:
data("ToothGrowth") View(ToothGrowth) filtered_tg <- subset(ToothGrowth, dose == 0.5) View(filtered_tg)
用直接子集索引:
data("ToothGrowth") View(ToothGrowth) filtered_tg <- ToothGrowth[ToothGrowth$dose == 0.5, ] View(filtered_tg)
内容的提问来源于stack exchange,提问作者Alejandro López
相关产品推荐
相关产品推荐

