r - 如何在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"))
我想删除数据帧中的共同峰值,并获得所需的输出:
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 个数据帧上苦苦挣扎。
解决方案
使用以下方法:
#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)
推荐阅读
- android - Android 条纹集成
- ember.js - 从子路由中的组件触发对父路由的操作
- algorithm - 澄清答案......在一个集合中找到最大可能的两个相等和
- php - 如何使用 laravel 5 跳过某些路线上的视图缓存?
- sql - 在一个 SQL 中将行转换为列,但不影响行数
- django - 如何在 django 中保留两个身份验证系统
- amazon-web-services - Amazon SES - 是否有内置选择退出/取消订阅选项可用
- r - R中的randomForest:可以拟合模型并将其用于没有错误的预测,但tuneRF会给出差异长度误差
- xml - 如何更改 fo:* 元素中的属性
- javascript - javascript - 如何在列中绘制框