gpt4 book ai didi

r - 仅保留在其至少一列中包含某些条目的行

转载 作者:行者123 更新时间:2023-12-05 03:26:54 25 4
gpt4 key购买 nike

我有以下数据框 df

structure(list(Names = c("James", "Diana", "Dr. Michel", "Mr. George", "Hello world", "Janis","JJK"), code = c("xx34-2111", "7789-2869", "dg67-5555", "1555-2uuy", "fhgj-2jj8", "2199-7kk8","1111-1111"), `13/A1` = c(NA, "‡(2017)", NA, "‡(2017)", "A", 
"A(2019)", "A(2016)"), `13/A2` = c(NA, "‡(2017)", NA, "‡(2017)",
"A", "A(2019)", "A(2016)"), `13/A3` = c(NA, "‡(2017)", NA, "‡(2017)",
"A", "A(2019)", "A(2016)"), `13/A4` = c(NA, "‡(2017)", NA, "‡(2017)",
"A", "A(2019)", "A(2016)"), `13/A5` = c(NA, "‡(2017)", NA, "‡(2017)",
"A", "A(2019)", "A(2016)"), `13/B1` = c("A", "A", "A(2017)",
"‡(2017)", "A", "A(2019)", NA), `13/B2` = c("A", "A", "A(2017)",
"‡(2017)", "A", "A(2019)", NA), `13/B3` = c("A", "A", "A(2017)",
"‡(2017)", "A", "A(2019)", NA), `13/B4` = c("A", "A", "A(2017)",
"‡(2017)", "A", "A(2019)", NA), `13/B5` = c("A", "A", "A(2017)",
"‡(2017)", "A", "A(2019)", NA), `13/C1` = c(NA, "A", NA, "‡(2017)",
"A", "A(2019)", NA), `13/D1` = c(NA, "‡(2017)", NA, "‡(2017)",
NA, "A", NA), `13/D2` = c(NA, "‡(2017)", NA, "‡(2017)", NA, "A",
NA), `13/D3` = c(NA, "‡(2017)", NA, "‡(2017)", NA, "A", "A(2017)"
), `13/D4` = c("A", "‡(2017)", NA, "‡(2017)", NA, "A", NA)), row.names = c(1L,
6L, 11L, 39L, 101L, 483L, 667L), class = "data.frame")

我只想保留 df 的行,这些行至少包含 3:17 中的一列,以下任何条目:A A(2016)A(2017)A(2019)

对于上面的示例,这意味着只删除包含 Mr. 的行。 George 在字段 Names 中。

我的尝试

df[,3:17]%in% c("A","A(2016)","A(2017)","A(2019)")

没有给出想要的结果。

最佳答案

遍历列 - sapply,检查值是否匹配 - %in%,然后检查每行是否有任何列 TRUE - rowSums(...) > 0

df[ rowSums(sapply(df[, 3:17], function(i) i %in% c("A","A(2016)","A(2017)","A(2019)"))) > 0, ]

关于r - 仅保留在其至少一列中包含某些条目的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71600334/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com