r - Erase repeated lines in r with a conditional column
问题描述
I have this data: https://pastebin.com/x8HrT8qK
"sp";"mes";"ano";"code"
"56";"CM";7;2016;"CM52"
"57";"CM";2;2019;"CM52"
"58";"CM";11;2016;"CM53"
"59";"CM";9;2019;"CM53"
"60";"CM";5;2018;"CM53"
"61";"CM";5;2018;"CM53"
"374";"EI";8;2019;"EI26"
"375";"EI";8;2019;"EI26"
"376";"EI";3;2019;"EI26"
"377";"EI";7;2019;"EI26"
"378";"EI";11;2019;"EI26"
"379";"EI";2;2020;"EI26"
"380";"EI";10;2019;"EI27"
"381";"EI";11;2019;"EI27"
"382";"EI";11;2019;"EI27"
and I would like to exclude lines that have the same "code" only if they have the same "ano"
So that the data would look like this: https://pastebin.com/F7tkUZE1
"sp";"mes";"ano";"code"
"56";"CM";7;2016;"CM52"
"57";"CM";2;2019;"CM52"
"58";"CM";11;2016;"CM53"
"59";"CM";9;2019;"CM53"
"60";"CM";5;2018;"CM53"
"374";"EI";8;2019;"EI26"
"379";"EI";2;2020;"EI26"
"380";"EI";10;2019;"EI27"
解决方案
We can use duplicated
to create a logical index of duplicate elements and then remove the rows based on the index. Using base R
(without any external packages)
df1[ !duplicated(df1[c('ano', 'code')]),]
# sp mes ano code
#56 CM 7 2016 CM52
#57 CM 2 2019 CM52
#58 CM 11 2016 CM53
#59 CM 9 2019 CM53
#60 CM 5 2018 CM53
#374 EI 8 2019 EI26
#379 EI 2 2020 EI26
#380 EI 10 2019 EI27
data
df1 <- read.csv('file.csv', sep=";")
df1 <- structure(list(sp = c("CM", "CM", "CM", "CM", "CM", "CM", "EI",
"EI", "EI", "EI", "EI", "EI", "EI", "EI", "EI"), mes = c(7L,
2L, 11L, 9L, 5L, 5L, 8L, 8L, 3L, 7L, 11L, 2L, 10L, 11L, 11L),
ano = c(2016L, 2019L, 2016L, 2019L, 2018L, 2018L, 2019L,
2019L, 2019L, 2019L, 2019L, 2020L, 2019L, 2019L, 2019L),
code = c("CM52", "CM52", "CM53", "CM53", "CM53", "CM53",
"EI26", "EI26", "EI26", "EI26", "EI26", "EI26", "EI27", "EI27",
"EI27")), class = "data.frame", row.names = c("56", "57",
"58", "59", "60", "61", "374", "375", "376", "377", "378", "379",
"380", "381", "382"))
推荐阅读
- c# - 从 .NET Core Program.cs 访问配置
- amazon-web-services - Http 到 Https 重定向在 AWS 中不起作用
- java - 初始化对象数组网格时出现空点错误
- android - 错误:使用 avdmanager 时找不到匹配的设备 --device Pixel 3a
- c++ - 在文件中查找单词 c++
- symfony - CKEditor 不能在 Symfony 4.4 上使用 EasyAdmin 3
- nlp - 如何使用 NLP 从成分中提取主要食物词?
- javascript - 当您分发项目时,NPM 是否会自动从您的 package.json 下载所有依赖项?
- list - 如何在多个索引处用不同的字符替换列表中的项目?
- r - 将不均匀的 json 解析为 tibble