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

R语言已提取指定列NA行子集成变量后如何更新原数据框?

解决方案

你可以通过以下几种方式实现需求,按需选择即可:

  • 方式1:直接筛选非NA行(最常用,无需依赖已生成的missing_weight变量)
    直接对weight列的NA判断结果取反,保留非NA的行覆盖原数据框:
baseball <- subset(baseball, !is.na(weight))
  • 方式2:基于已提取的missing_weight移除对应行
    如果你需要复用已经生成的missing_weight对象,可以通过行名匹配排除对应行:
baseball <- baseball[!rownames(baseball) %in% rownames(missing_weight), ]

小提示:只要你提取missing_weight后没有改动过baseball的行顺序,默认的数字行名也可以作为匹配依据,不会出现匹配错误。

  • 方式3:tidyverse风格写法(适合习惯dplyr语法的场景)
    加载dplyr包后用drop_na()指定检查列即可:
library(dplyr)
baseball <- baseball %>% drop_na(weight)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 20:24:04