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

如何根据列名片段删除DataFrame中含CNT的指定列?

删除DataFrame中列名含"CNT"的列

方法一:使用tidyverse(dplyr)

你之前用mutate是找错了函数——mutate用于新增或修改列,删除列得用select:

library(dplyr)

# 假设你的数据框名为df
df_cleaned <- df %>%
  select(-contains("CNT"))

如果需要更精准匹配(确保"CNT"出现在列名中间,而非首尾),可以用正则匹配的matches,效果和contains在你的场景下一致:

df_cleaned <- df %>%
  select(-matches("CNT"))

方法二:使用Base R

不用加载额外包的话,直接通过列名筛选即可:

# 保留列名不含"CNT"的列
df_cleaned <- df[, !grepl("CNT", colnames(df))]

或者用subset函数实现:

df_cleaned <- subset(df, select = !grepl("CNT", colnames(df)))

内容的提问来源于stack exchange,提问作者D. Fowler

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 20:30:56