首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在R的5个数据框中删除列中的常见元素

如何在R的5个数据框中删除列中的常见元素
EN

Stack Overflow用户
提问于 2021-04-21 22:13:48
回答 1查看 23关注 0票数 1

我有5个数据帧:

代码语言:javascript
复制
a <- data.frame(ID = c("1", "2", "3", "4", "5"), peak = c("peak1", "peak2", "peak3", "peak4", "peak10"))
b <- data.frame(ID = c("1", "2", "3", "4"), peak = c("peak1","peak3", "peak20", "peak21"))
c <- data.frame(ID = c("1", "2", "3"), peak = c("peak1", "peak5", "peak3"))
d <- data.frame(ID = c("1", "2", "3", "4", "5", "6"),peak = c("peak1", "peak3", "peak7", "peak8", "peak11", "peak12"))
e <- data.frame(ID = c("1", "2", "3"), peak = c("peak1", "peak3",  "peak9"))

我想要删除数据帧之间的公共峰值,并具有所需的输出:

代码语言:javascript
复制
a <- data.frame(ID = c("1", "2", "3", "4", "5"), peak = c("peak2", "peak4", "peak10"))
b <- data.frame(ID = c("1", "2", "3", "4"), peak = c("peak20", "peak21"))
c <- data.frame(ID = c("1", "2", "3"), peak = c("peak5", ))
d <- data.frame(ID = c("1", "2", "3", "4", "5", "6"),peak = c(  "peak7", "peak8", "peak11", "peak12"))
e <- data.frame(ID = c("1", "2", "3"), peak = c(  "peak9"))

我知道如何比较两个数据帧a[!(a$peak %in% b$peak),],但我正在为5而苦苦挣扎。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-21 22:17:18

使用以下方法:

代码语言:javascript
复制
#Put the data in a list
list_df <- dplyr::lst(a, b, c, d, e)
#Get the common peak value
common_peak <- Reduce(intersect, lapply(list_df, `[[`, 'peak'))
common_peak
#[1] "peak1" "peak3"

#Remove the common peak value from all the dataframes
result <- lapply(list_df, function(x) subset(x, !peak %in% common_peak))
result

#$a
#  ID   peak
#2  2  peak2
#4  4  peak4
#5  5 peak10

#$b
#  ID   peak
#3  3 peak20
#4  4 peak21

#$c
#  ID  peak
#2  2 peak5

#$d
#  ID   peak
#3  3  peak7
#4  4  peak8
#5  5 peak11
#6  6 peak12

#$e
#  ID  peak
#3  3 peak9

#Update all the individual dataframes
list2env(result, .GlobalEnv)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67197632

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档