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

如何获取新样本存在但历史样本不存在的物种列名?

提取新增物种列名的方法

不需要专门的函数,用基础R或者tidyverse工具就能轻松实现,以下是具体方案:

基础R实现

# 提取历史样本和新样本的物种数据
hist_data <- df[df$sample == "hist", grep("species", names(df))]
new_data <- df[df$sample == "new", grep("species", names(df))]

# 筛选符合条件的列名
added_species <- names(new_data)[new_data == 1 & hist_data == 0]
added_species
# 输出结果:"speciesC"

tidyverse实现(dplyr + tidyr)

如果你习惯用tidyverse生态,可以这样写:

library(tidyverse)

df %>%
  pivot_longer(cols = starts_with("species"), names_to = "species", values_to = "presence") %>%
  pivot_wider(names_from = sample, values_from = presence) %>%
  filter(new == 1 & hist == 0) %>%
  pull(species)
# 输出结果:"speciesC"

两种方法都能精准筛选出new样本为1、hist样本为0的物种列名,根据你的使用习惯选择即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 22:34:57