You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言求助:如何删除auto-mpg数据集中指定名称的样本行?

解决删除auto-mpg数据集中指定行的问题

你之前的错误在于混淆了列操作和行操作:select()函数的作用是选择或删除列,而"plymouth reliant"是name列里的一个样本值,不是列名,所以才会报“列不存在”的错误。

要删除指定名称的样本(行),可以用以下两种方法:

方法1:使用dplyr的filter函数

先确保加载了dplyr包,然后通过条件筛选排除目标行:

library(dplyr)
# 排除name列等于"plymouth reliant"的样本
autompg = autompg %>% filter(name != "plymouth reliant")

方法2:基础R写法(无需额外包)

直接通过行索引筛选:

# 保留name列不等于"plymouth reliant"的所有行
autompg = autompg[autompg$name != "plymouth reliant", ]

如果存在多个匹配"plymouth reliant"的样本,两种方法都会一次性删除所有符合条件的行。

内容的提问来源于stack exchange,提问作者Ankita

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 18:32:10