如何删除R中5个数据框中列中的常见元素 [英] how to remove common elements in a column in 5 data frame in R

查看:33
本文介绍了如何删除R中5个数据框中列中的常见元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 5 个数据框:

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"))

我想删除数据帧中的常见峰值,并具有所需的输出:

I would like to remove the common peaks across the data frames, with a desired outputs:

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 中挣扎.

I know how to compare two data frames a[!(a$peak %in% b$peak),] but I'm struggling with 5.

推荐答案

使用以下方法:

#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)

这篇关于如何删除R中5个数据框中列中的常见元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆