简体   繁体   English

如何保留重复项,但根据R中的列删除唯一值

[英]How do I keep duplicates but remove unique values based on column in R

How can I keep my duplicates, but remove unique values based on one column(qol)? 如何保留重复项,但基于一列(qol)删除唯一值?

ID   qol  Sat
A     7   6
A     7   5
B     3   3 
B     3   4
B     1   7
C     2   7
c     1   2

But I need this: 但我需要这个:

ID   qol  Sat
A     7   6
A     7   5
B     3   3 
B     3   4

What can I do? 我能做什么?

dplyr solution: dplyr解决方案:

library(dplyr)

ID <- c("A", "A", "B", "B", "B", "C", "c")
qol <- c(7,7,3,3,1,2,1)
Sat <- c(6,5,3,4,7,7,2)

test_df <- data.frame(cbind(ID, qol, Sat))

filtered_df <- test_df %>%
               group_by(qol) %>%
               filter(n()>1)

Please note that this will return 请注意,这将返回

       ID    qol    Sat
1      A      7      6
2      A      7      5
3      B      3      3
4      B      3      4
5      B      1      7
6      c      1      2

If you also want to remove the two rows where qol == 1 but the IDs are different, just do: 如果您还想删除qol == 1但ID不同的两行,只需执行以下操作:

filtered_df <- test_df %>%
               group_by(ID, qol) %>%
               filter(n()>1)

This will return the sample output you supplied in the question. 这将返回您在问题中提供的示例输出。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据三列删除重复项,但我使用 R 保留特定列中编号最高的行? - How do I remove duplicates based on three columns, but I keep the row with the highest number in the specific column using R? 如何删除唯一条目并在R中保留重复项 - how to remove unique entry and keep duplicates in R R编程:如何根据另一列的值删除一列中的重复项 - R programming : How to remove Duplicates in a column based on values of another column 如何根据 R 中数据框中的某些列和行信息删除唯一行 - How do I remove unique rows based on certain column and row information in a data frame in R 如何根据一列中的重复项和另一列中的唯一值对 R 数据框进行子集化 - How to subset R data frame based on duplicates in one column and unique values in another 如何删除 R 中的重复项? - How do I remove duplicates in R? 删除重复项但保留基于特定列的行 - remove duplicates but keep the row based on a specific column 如何在向量的每个字符串中仅保留唯一的单词? 即删除所有重复项 - How do I keep only unique words within each string in a vector? I.e. remove ALL duplicates 在 R 中,如何根据多列中的时间和日期值创建具有唯一值的列 - In R how do you create a column with unique values based on time and date values in multiple columns 如何根据唯一的行值将列值合并到一列向量中? - How do I merge column values into a column of vectors based on unique row values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM